【问题标题】:UITableView crashes when datasource is connected in Interface Builder在 Interface Builder 中连接数据源时 UITableView 崩溃
【发布时间】:2011-07-08 22:43:48
【问题描述】:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)cijferTableView{
return 1;
}
- (NSInteger)cijferTableView:(UITableView *)cijferTableView numberOfRowsInSection:(NSInteger)section {
    return [marksArray count];
}
- (UITableViewCell *)cijferTableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [marksArray objectAtIndex:indexPath.row];

return cell;
}

我有一个用字符串填充的标记数组。 该代码在一刻钟前运行良好,但从那以后,当我加载该代码所在的视图时它一直在崩溃,而我没有进行任何更改。

但是,当我在界面生成器中断开数据源时,视图会正确加载而不会崩溃。但当然,在这种情况下它不会填满表格。

我做错了什么?


更新:

控制台给出的错误是在抛出“NSException”实例后调用终止

另外,我还没有在marksArray 中添加任何东西。为了测试,我只有这个:

 //.h

NSMutableArray *marksArray;

//.m

marksArray = [NSMutableArray arrayWithObjects:@"1", @"2", nil;

【问题讨论】:

  • 能否请您发布控制台给您的错误?
  • 其实你也可以把初始化marksArray的代码贴出来吗?听起来您可能正在访问一些无效数据。
  • @Jensen2k @Jablair 更新了第一篇文章。

标签: iphone xcode ios4 interface-builder ios-simulator


【解决方案1】:

看起来您使用 cijferTableView 搜索并替换了“tableView”,并在这样做时重命名了方法,这将导致它中断。例如:

- (NSInteger)cijferTableView:(UITableView *)cijferTableView numberOfRowsInSection:(NSInteger)section {
    return [marksArray count];
}

应该是……

- (NSInteger)tableView:(UITableView *)cijferTableView numberOfRowsInSection:(NSInteger)section {
    return [marksArray count];
}

【讨论】:

  • 哇,我有没有跳过这个问题 - 直接跳到数组并没有真正阅读代码。为了扩展 Kenny 的答案,您声称您实现了 UITableViewDataSource,但您没有实现这些方法 - 本质上,您违反了合同。结果,应用程序尝试调用 UITableViewDataSource 方法并引发运行时错误,因为您的控制器没有实现选择器。
  • 你能更新你的帖子并添加你的新代码吗?您在崩溃时收到任何类型的错误消息吗?
【解决方案2】:

1) 你忘了保留marksArray

2) dataSource 方法的奇怪名称('cijfer' 东西而不是 tableView:numberOfRowsInSection: 和 tableView:cellForRowAtIndexPath:)。他们不会工作。

【讨论】:

  • 天哪,我是个白痴。你是对的,我忘了保留marksArray。感谢您的帮助!
【解决方案3】:

为什么要重命名委托方法?也许这些是导致您的一些问题的原因?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 2012-09-16
    • 2010-12-07
    相关资源
    最近更新 更多