我正在尝试显示我在 InterfaceBuilder 中创建的两个不同的 UITableViewCell,但是我收到了这个错误。
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
我创建了两个新的 Nib 我已经将文件所有者类更改为我试图在其中显示它们的 UITableViewController,并使用 IBOutlets 等将它们连接起来。
错误发生在 TableView:cellForRowAtIndexPath 中,这就是我在该类中的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(indexPath.section == 0) {
// Manufactures --->
if (indexPath.row == 0) {
cell = nil;
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CodeSearchTextCell" owner:self options:nil];
cell = codeSearchTextCell; //Loads tableviewcells with custome xib-cell that I have made in Interface builder
self.codeSearchTextCell = nil;
}
codeTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
codeTextField.layer.borderWidth = 0.5f;
cell.userInteractionEnabled = NO;
}
else if (indexPath.row == 1) {
cell = nil;
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CodeSearchCell" owner:self options:nil];
cell = codeSearchCell; //Loads tableviewcells with custome xib-cell that I have made in Interface builder
self.codeSearchCell = nil;
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //adds disclosure indicator
cell.userInteractionEnabled = YES;
}
}
return cell;
}
我不确定我做错了什么,但是当我将断点放在最后一行时,我可以清楚地看到我返回单元格,它返回时没有任何值(value)。
最佳答案
在此示例中,我假设您有一个名为 CodeSearchCell
的 UIView
子类:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if (0 == indexPath.section) {
if (0 == indexPath.row) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CodeSearchTextCell" owner:self options:nil];
CodeSearchTextCell *codeSearchTextCell = [tableView dequeueReusableCellWithIdentifier:@"CodeSearchCell"];
if ( nil == codeSearchTextCell ) {
codeSearchTextCell = (CodeSearchTextCell *)[nib objectAtIndex:0];
}
cell = codeSearchTextCell;
} else if (1 == indexPath.row) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CodeSearchCell" owner:self options:nil];
CodeSearchCell *codeSearchCell = [tableView dequeueReusableCellWithIdentifier:@"CodeSearchCell"];
if ( nil == codeSearchCell ) {
codeSearchCell = (CodeSearchCell *)[nib objectAtIndex:0];
}
cell = codeSearchCell;
} else {
// If indexPath.row > 1
// Add logic to make sure cell isn't nil.
} else {
// If indexPath.section > 0
// Add logic to make sure cell isn't nil.
}
return cell;
}
关于iphone - UITableView 数据源必须返回一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19371909/