UITableView dataSource必须返回一个单元格

时间:2013-10-15 01:29:48

标签: iphone ios uitableview

我正在尝试显示我在InterfaceBuilder中创建的两个不同的UITableViewCell,但是我收到了这个错误。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

我创建了两个新的笔尖我已经将文件所有者类更改为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;
}

我不确定我做错了什么,但是当把断点放在最后一行时我可以清楚地看到我返回单元格时它没有返回值。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

在这个例子中,我假设你有一个名为UIView的{​​{1}}子类:

CodeSearchCell

答案 1 :(得分:0)

我认为你应该这样试试希望它会帮助你

- (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) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CodeSearchTextCell" owner:self options:nil] objectAtIndex:0];
            }
            codeTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
            codeTextField.layer.borderWidth = 0.5f;
            cell.userInteractionEnabled = NO;
        }
        else if  (indexPath.row == 1) {
            cell = nil;
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CodeSearchCell" owner:self options:nil] objectAtIndex:0];
            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //adds disclosure indicator
            cell.userInteractionEnabled = YES;
        }
    }
    return cell;
}