gpt4 book ai didi

iphone - NSMutableArray 没有添加所有项目,只是最后一个 - Iphone/IOS/xcode

转载 作者:行者123 更新时间:2023-11-28 18:26:28 26 4
gpt4 key购买 nike

我正在使用以下方法将 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

所以我肯定只是添加了错误的数组!

This is what's returned

任何帮助将不胜感激

最佳答案

你不断地创建新的、空的 NSMutableArrays

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

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

break;
}

要么预先分配/初始化 arryTableAllIcons,要么检查它不是 nil

    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);
}

关于iphone - NSMutableArray 没有添加所有项目,只是最后一个 - Iphone/IOS/xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8035320/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com