iphone - 代码适用于 xib,但不适用于 Storyboard

标签 iphone objective-c uitableview ios6 exc-bad-access

我是 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 运行时,我到达屏幕下方。

enter image description here

注意事项:

我也去了模拟器并做了重置内容和设置,仍然面临同样的问题。知道为什么吗?


更新1

我激活 NSZombieEnabled 并运行代码。我以

的身份进入 NSLOG
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

下面是屏幕截图。

enter image description here


更新 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/

相关文章:

ios - 由于未捕获的异常,SIGABRT 终止应用程序

iPhone:自定义 DataGrid View 、子类 UITableView 或其他?

ios - 重新加载 UITableView 数据后,indexPathForCell 返回 null

iphone - 我可以构建一个 ARC 框架并将其用于非 ARC 项目吗?

iphone - Instruments VM 跟踪器为空

iphone - 我是否需要发布复制的 NSObjects - Objective-c

objective-c - 重复符号错误

ios - 如何为 UICollectionView 实现放大/缩小动画

ios - Textview Frame 被挤压无法恢复原状?

ios - 如何确定 NSDate 是否是工作日?