繁体   English   中英

在Objective C中解析二维数组

[英]Parsing a two dimensional array in Objective C

我已经制作了一个iOS应用程序,该应用程序正在解析填充UITableView的单个维度数组,我尝试将两个条目(来自XML的文件的“名称”和文件的“ URL”)发送到该数组。 但是在“表”视图中填充了“名称”和URL。 我想将名称显示为单元格文本,并将URL显示为单元格详细信息文本。 有什么帮助吗?

 (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(@"%@",string);
if(count){
    listset1=[[NSMutableArray alloc]initWithCapacity:20];
    count=0;
}


if ([currentElementValue isEqualToString:@"Name"]) {
    [Name appendString:string];
    [listset1 addObject:[NSString stringWithFormat:@"%@",Name]];
        //    NSLog(@"%@",listset1);
}

if ([currentElementValue isEqualToString:@"URL"]) {
    [URL appendString:string];
    [listset1 addObject:[NSString stringWithFormat:@"%@",URL]];
    //NSLog(@"%@",URL);

}

Listset1是要解析的数组。

用Objective-C语言是不可能的。

您可以采用两个数组,也可以引用“类”结构。

我更喜欢您的班级结构。

创建一个名称NSObject的类类型,其中两个对象为NSString作为NameURL

在获得价值的同时,

sampleClass *objClass = [[sampleClass alloc] init];

objClass.strName = [NSString stringWithFormat:@"%@", strName];
objClass.strURL = [NSString stringWithFormat:@"%@", strURL];

[listArray addObject: objClass];

在显示数据时,

for (int i = 0; i < [listArray count]; i++)
{
   objClass = [listArray objectAtIndex:i];

   NSLog(@" --> %@", objClass.strName);
   NSLog(@" --> %@", objClass.strURL);
}

希望,你会明白的。

谢谢。

你在商店名称和网址NSMutableDictionary和存储NSMutableArray

  -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

        if(count){
           NSMutableArray* listset1=[[NSMutableArray alloc]initWithCapacity:20];
            count=0;
        }

         NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        if ([currentElementValue isEqualToString:@"Name"]) {
            [Name appendString:string];
            [dict setValue:[NSString stringWithFormat:@"%@",Name] forKey:@"Name"];
        }

        if ([currentElementValue isEqualToString:@"URL"]) {
            [URL appendString:string];
            [dict setValue:[NSString stringWithFormat:@"%@",URL] forKey:@"URL"];

        }
        [listset1 addObject:dict];
        [dict release];
    }

多维数组不是可能的。

假设您有两个不同的数组,并且保留了两者之间的语义。 所以name [0]对应于url [0]

现在在您的viewcontroller中实现以下方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *CellIdentifier;
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textlabel.text=name[indexPath.row];
cell.detailTextLabel.text=url[indexPath.row];

return cell;


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM