簡體   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