【发布时间】:2010-04-23 09:20:11
【问题描述】:
我正在尝试使用自定义 UITableViewCell 构建我的应用程序。这是我的UIViewController 中的代码,它将viewCell 添加到表中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"------- Tableview --------");
static NSString *myIdentifier = @"MyIdentifier";
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
if(cell == nil) {
NSArray *cellView = [[NSBundle mainBundle] loadNibNamed:@"tblCellView" owner:self options:nil];
cell = [cellView objectAtIndex:0];
}
[cell setLabelText:[NSString stringWithFormat:@"indexpath.row: %d", indexPath.row]];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myIdentifier] autorelease];
cell.textLabel.text = @"bös";
return cell;
}
如果我取消注释“返回单元格”上方的行,它会返回一个常规的 UITableViewCell 而没有任何错误,但是一旦我尝试实现我的自定义单元格,它就会崩溃并出现以下错误:
------- Tableview -------- 2010-04-23 11:17:33.163 Golf[26935:40b] * -[UITableView] 中的断言失败 _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-984.38/UITableView.m:4709 2010-04-23 11:17:33.164 Golf[26935:40b] * 由于未捕获而终止应用程序 异常'NSInternalInconsistencyException',原因:'UITableView dataSource 必须从 tableView 返回一个单元格:cellForRowAtIndexPath:' 2010-04-23 11:17:33.165 高尔夫[26935:40b] 堆栈:(
...
我已按照应有的方式配置了 .xib 文件,并使用了正确的插座。并且 UITableViewCell 的标识符对应于我试图从 NSBundle 加载的名称
【问题讨论】:
-
尝试将其转换为 (MyTableCell *)
-
不要忘记在 .m 文件中导入自定义单元格并与自定义单元格绑定。还要检查 tableview 代表的数据源。
标签: iphone objective-c cocoa xcode macos