gpt4 book ai didi

iphone - 从 NSDictionary 填充表格 View

转载 作者:行者123 更新时间:2023-11-28 22:29:19 25 4
gpt4 key购买 nike

我想用我相信的 NSDictionary 填充动态表格 View 的单元格,这是我填充表格 View 的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ResultsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSData *jsonData = self.responseFromServer;

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSArray *results = [json objectForKey:@"myData"];
for (NSDictionary *item in results) {
cell.title.text =[[item objectForKey:@"title"]objectAtIndex:indexPath.row];
}
// Configure the cell...

return cell;
}

如果我有

cell.title.text =[item objectForKey:@"title"];

几乎可以工作,但我所有的标题都是一样的。但是目前我得到的错误是什么:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x7612940'

我不确定这意味着什么或如何修复它。

最佳答案

看起来你的字典实际上是一个字典数组,每个字典都有键@"Title"。

您现在正在做的是获取每个元素的 String 并尝试获取 indexPath.row 的索引,但字符串没有该方法。

由于您只需要索引indexPath.row 处的对象,因此您可以将整个for 循环替换为以下代码行:

 cell.title.text = [[results objectAtIndex:indexPath.row] objectForKey:@"title"];

此外,正如 Nicholas Hart 所说,为了大大提高性能,您应该在收到 json 对象时在代码中添加以下几行,以便它只执行一次,并使结果成为一个实例变量,可以从 tableView 的委托(delegate)方法访问:

NSData *jsonData = self.responseFromServer;

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
NSArray *results = [json objectForKey:@"myData"];

关于iphone - 从 NSDictionary 填充表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17932504/

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