iphone - UITableView 数据源必须返回一个单元格

标签 iphone ios uitableview

我正在尝试显示我在 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)。

最佳答案

在此示例中,我假设您有一个名为 CodeSearchCellUIView 子类:

- (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/

相关文章:

ios - 将音频 .wav 文件从 http 服务器流式传输到 ios 客户端

ios - 解除 UIActivityViewController 后的操作

ios - UITableViewCell 将消失

ios - swift UITableView 中的 didSelectRow 事件延迟

iphone - 需要确认继承 UIAlertView 是合法的

ios - 实时读取 Apple Watch 系统日志 (NSLog())

iphone - UIView的UIImagePickerController

ios - 在 Swift 中使用 AFNetworking 接收响应

ios - 仅为一个 session 存储数据的变量

ios - 根据 JSON 文件中的内容文本调整带有 UILabel 的自定义单元格的大小