我正在尝试使用自定义 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] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-984.38/UITableView.m:4709 2010-04-23 11:17:33.164 Golf[26935:40b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 2010-04-23 11:17:33.165 Golf[26935:40b] Stack: (
...
我已经按照正确的 socket 配置了 .xib 文件。 UITableViewCell 的标识符与我尝试从 NSBundle 加载的名称相对应
最佳答案
您还应该确保将 Interface Builder 中的自定义类类型设置为相应的类名称(在您的示例中为 MyTableCell
),否则转换也将不起作用。
关于iphone - 添加自定义 UITableViewCell 会使模拟器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2697428/