2011-11-07 26 views
0

我使用下面的加载图标从一个XML文件中的一个NSMutableArray:NSMutableArray中不加入的所有项目,刚刚过去的一个 - iPhone/IOS/Xcode的

NSArray *iconItems = [doc nodesForXPath:kName_icon error:nil];//Root node 
    for (CXMLElement *iconItem in iconItems) 
    { 

     NSArray *iconTempArray = [iconItem elementsForName:kName_url]; 
     for(CXMLElement *urlTemp in iconTempArray) 
     { 
      arryTableAllIcons = [[NSMutableArray alloc] init]; 
      [arryTableAllIcons addObject:[NSString stringWithFormat:@"%@", urlTemp.stringValue]]; 

      NSLog(@"Icon Found %@",urlTemp.stringValue); 

     break; 
     } 

我想通过以下在我的表显示此:(这是在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

cell.textLabel.text = [arryTableAllIcons objectAtIndex:indexPath.row]; 

计数似乎工作,因为我有细胞数正确,但发现空单元格中的表1与最后一个图标

The count is just: `return [arryTableAllIcons count];` 

而我的NSLog带回正确的文本

2011-11-07 10:30:23.692 Del Search[2791:f203] Icon Found guideDogs_off.png 
2011-11-07 10:30:23.692 Del Search[2791:f203] Icon Found WheelchairAssist_off.png 
2011-11-07 10:30:23.739 Del Search[2791:f203] Icon Found walk_off.png 
2011-11-07 10:30:23.740 Del Search[2791:f203] Icon Found DisabledWc_off.png 
2011-11-07 10:30:23.741 Del Search[2791:f203] Icon Found hearingaid_off.png 
2011-11-07 10:30:23.741 Del Search[2791:f203] Icon Found loop_off.png 
2011-11-07 10:30:23.742 Del Search[2791:f203] Icon Found carpark_off.png 
2011-11-07 10:30:23.742 Del Search[2791:f203] Icon Found dropcounter_off.png 
2011-11-07 10:30:23.743 Del Search[2791:f203] Icon Found staff_off.png 
2011-11-07 10:30:23.743 Del Search[2791:f203] Icon Found Buggy_off.png 

所以我必须仅仅是添加阵列错了!

This is what's returned

任何帮助将是非常赞赏

回答

2

我认为问题是,你声明“对于”循环内的数组。这样你每次实例化它的循环重复

+1

你这个小天才,谢谢你。另一件事,现在它正在工作,它底部显示3个空白单元格?任何想法为什么 – TMB87

+0

所以投我:) –

+0

我现在看看你的代码.....你NSLogging的返回 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分? –

3

你不断创造新的空NSMutableArrays

for(CXMLElement *urlTemp in iconTempArray) 
    { 
     arryTableAllIcons = [[NSMutableArray alloc] init]; 
     [arryTableAllIcons addObject:[NSString stringWithFormat:@"%@", urlTemp.stringValue]]; 

     NSLog(@"Icon Found %@",urlTemp.stringValue); 

    break; 
    } 

无论是分配/初始化arryTableAllIcons提前,或检查它不是零

for(CXMLElement *urlTemp in iconTempArray) 
    { 
     if (!arryTableAllIcons) 
      arryTableAllIcons = [[NSMutableArray alloc] init]; 
     [arryTableAllIcons addObject:[NSString stringWithFormat:@"%@", urlTemp.stringValue]]; 

     NSLog(@"Icon Found %@",urlTemp.stringValue); 

    break; 
    } 

也是break语句将退出对第一通环路封闭的,所以我认为它不应该存在

for(CXMLElement *urlTemp in iconTempArray) 
    { 
     if (!arryTableAllIcons) 
      arryTableAllIcons = [[NSMutableArray alloc] init]; 
     [arryTableAllIcons addObject:[NSString stringWithFormat:@"%@", urlTemp.stringValue]]; 

     NSLog(@"Icon Found %@",urlTemp.stringValue); 
    } 
+0

+1,你打败了我。 – LouwHopley

+0

非常感谢这位伴侣,你能告诉我为什么它现在在底部创建3个空白单元格吗?这很奇怪 – TMB87

+0

很难告诉你给的信息,试着在for循环中放一个断点,看看它为什么会创建额外的空项目。 – jbat100

1

在这部分代码:

for(CXMLElement *urlTemp in iconTempArray) 
{ 
    arryTableAllIcons = [[NSMutableArray alloc] init]; 
    [arryTableAllIcons addObject:[NSString stringWithFormat:@"%@", urlTemp.stringValue]]; 
    NSLog(@"Icon Found %@",urlTemp.stringValue); 
    break; 
} 

您是重新init上通过for循环每一步-ing新arryTableAllIcons

除了创建内存泄漏,您还在创建大量新阵列。最后一个实例是具有最后一个项目的实例。

将循环外部的alloc-init语句完全移出(完全可能在方法外部,并且移至类init),并确保您在使用它之后的某个时刻release

相关问题