我使用以下代码将图标从XML文件加载到NSMutableArray中:
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所以我一定是把数组加错了!

任何帮助都将不胜感激
发布于 2011-11-07 18:45:12
我认为问题在于你在"For“循环中声明了数组。通过这种方式,您可以在每次循环重复时实例化它
发布于 2011-11-07 18:46:24
您不断创建新的、空的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循环,因此我认为它不应该在那里
for(CXMLElement *urlTemp in iconTempArray)
{
if (!arryTableAllIcons)
arryTableAllIcons = [[NSMutableArray alloc] init];
[arryTableAllIcons addObject:[NSString stringWithFormat:@"%@", urlTemp.stringValue]];
NSLog(@"Icon Found %@",urlTemp.stringValue);
}发布于 2011-11-07 18:50:30
在这部分代码中:
for(CXMLElement *urlTemp in iconTempArray)
{
arryTableAllIcons = [[NSMutableArray alloc] init];
[arryTableAllIcons addObject:[NSString stringWithFormat:@"%@", urlTemp.stringValue]];
NSLog(@"Icon Found %@",urlTemp.stringValue);
break;
}在for循环中的每一步,您都在重新init-ing一个新的arryTableAllIcons。
除了造成内存泄漏之外,您还创建了许多新的数组。最后一个实例化是具有最后一项的实例化。
将alloc init语句移到循环之外(可能完全在方法之外,并移到类init中),并确保在使用它之后的某个时刻对其执行release操作。
https://stackoverflow.com/questions/8035320
复制相似问题