TableView
报错解决方案与解释
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
未注册Cell,凡5.1版本起的xcode,纯代码写tableview,都要给返回的cell注册,无论是系统的还是自定义的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
<span style="color:#009900;"> static NSString *CellIdentifier = @"Cell";
</span><span style="color:#ff0000;"> [self.tableView registerClass:[</span><span style="color:#3366ff;">UITableViewCell</span><span style="color:#ff0000;"> class] forCellReuseIdentifier:CellIdentifier]; </span>
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// config cell
...
return cell;
}
绿色部分代码是标识cell,建议放到viewdidload方法上面;
红色部分代码则是注册cell,建议放到viewdidload方法里面,第一次进入页面就会注册cell 而不用每次返回 cell 去注册.
蓝色部分UITableViewCell是系统自带的cell,如果自定义cell也需要注册,把UITableViewCell改为自定义的cell类名即可;