简体   繁体   English

在Objective C中解析二维数组

[英]Parsing a two dimensional array in Objective C

I have made an iOS application that is parsing a single dimension array that populates a UITableView, i tried to send two entries to the array the "Name" of the File and the "URL" of the file from the xml. 我已经制作了一个iOS应用程序,该应用程序正在解析填充UITableView的单个维度数组,我尝试将两个条目(来自XML的文件的“名称”和文件的“ URL”)发送到该数组。 But in the Table view got populated with the Name and the URL. 但是在“表”视图中填充了“名称”和URL。 i want to display the name as the cell text and the url as the cell detail text. 我想将名称显示为单元格文本,并将URL显示为单元格详细信息文本。 Any help? 有什么帮助吗?

 (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 is the array that is being parsed. Listset1是要解析的数组。

It is not possible in objective-c language. 用Objective-C语言是不可能的。

Either you take two arrays or refer "Class" structure. 您可以采用两个数组,也可以引用“类”结构。

I'll prefer you class structure. 我更喜欢您的班级结构。

Create a class type of NSObject with two objects as NSString for Name and URL . 创建一个名称NSObject的类类型,其中两个对象为NSString作为NameURL

While getting value, 在获得价值的同时,

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

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

[listArray addObject: objClass];

While displaying data, 在显示数据时,

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

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

Hope, you'll understand. 希望,你会明白的。

Thanks. 谢谢。

you store name and url in NSMutableDictionary and store in NSMutableArray 你在商店名称和网址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];
    }

A multidimensional array isn't a possibilty. 多维数组不是可能的。

Let's Assume that you have two different arrays and you have preserved the semantics between the two. 假设您有两个不同的数组,并且保留了两者之间的语义。 So name[0] corresponds to url[0] 所以name [0]对应于url [0]

Now implement the following method in your viewcontroller 现在在您的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