0

我正在尝试显示我在 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;
}

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

任何帮助将不胜感激。

4

2 回答 2

2

在这个例子中,我假设你有一个UIView名为的子类CodeSearchCell

- (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;
}
于 2013-10-15T01:35:46.840 回答
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;
}
于 2013-10-15T04:20:47.390 回答