我是 iPhone 应用程序的新手。我正在学习如何获取 JSON 数据并对其进行解析并将其显示在 iPhone 的表格中。为此,我正在经历 this video
.
我用 xib 文件做了这个,它运行得很好。
我尝试的是用Storyboard
来做这件事。我对 Stoaryboard
做了同样的事情,但是执行在下一行中断了。
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
我的代码是
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"i m here 1");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
NSLog(@"i m here 2");
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
NSLog(@"i m here 3");
}
NSLog(@"i m here 4");
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
// cell.textLabel.text = @"dadsf ad";
NSLog(@"i m here 5");
return cell;
}
在 NSLog
中,我得到输出直到 i m here 4
。
当我评论 cell.textLabel.text = [[news objectAtIndex
并取消评论 cell.textLabel.text = @"dadsf ad";
时,它起作用了,我明白了iPhone 中的 dadsf 广告
。
// cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.textLabel.text = @"dadsf ad";
当我使用 cell.textLabel.text = [[news objectAt
运行时,我到达屏幕下方。
注意事项:
我也去了模拟器并做了重置内容和设置
,仍然面临同样的问题。知道为什么吗?
更新1
我激活 NSZombieEnabled
并运行代码。我以
JSONNewsStoryBoard[19389:11303] i m here 4
2013-01-06 11:26:15.327 JSONNewsStoryBoard[19389:11303] *** -[CFArray objectAtIndex:]: message sent to deallocated instance 0x7497040
下面是屏幕截图。
更新 2 - 答案
我尝试在 cellForRowAtIndexPath
中添加 NSLog(@"checking news count %d", [news count]);
我遇到了同样的问题。表示 news
为空。
所以我加了
news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
在 cellForRowAtIndexPath
中,它起作用了。 :) :)
最佳答案
似乎 news
对象在 objectAtIndex:
被调用之前的某个时间被释放了,这可能与界面构建器和 Storyboard采用的方式有关过度控制你给他们的东西。尝试检查代码中不同位置的 news
是否等于 nil
。您可能必须在不同的时间点实例化 news
。
关于iphone - 代码适用于 xib,但不适用于 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14180432/