invalid nib registered for identifier (CELLNAME) - nib must contain exactly one top level object which must be a UITableViewCell instance

后端 未结 14 2174
情书的邮戳
情书的邮戳 2020-12-14 00:03

Sorry for the long title, but I wanted it to be clearly seen from a google search. Also, this differs from many of the other similar questions on here as its not specifying

14条回答
  •  無奈伤痛
    2020-12-14 00:18

    The problem is that there are multiple cells in your storyboard that have the same name. Such as for a single table view, there are multiple cells that have the same identifier. In my case I had three cells that were all called 'prototypeCell'.

    The fix is actually quite easy. For every cell, name it a simple name with the cell number at the end. This cell number has to match the indexPath.row later on, so basically start from 0 onwards.

    For Example:

    prototypeCell0
    prototypeCell1
    prototypeCell2
    

    Then, go into the class thats responsible for the controller, and find the function

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    Then replace the code:

    static NSString *CellIdentifier = @"PrototypeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    With the code:

    static NSString *CellIdentifier = @"ListPrototypeCell";
    NSString* num = [NSString stringWithFormat:@"%d", indexPath.row];
    NSString* actual = [CellIdentifier stringByAppendingString:num];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:actual forIndexPath:indexPath];
    

    Everything else can stay the same. This code basically gets the row number, adds it to the identifier base, the gets the cell from the table using that string. So if its responding to a different row number, it return a different cell.

    Just for clarification, my whole function for the problem is as follows:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"ListPrototypeCell";
        NSString* num = [NSString stringWithFormat:@"%d", indexPath.row];
    
        NSString* actual = [CellIdentifier stringByAppendingString:num];
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:actual forIndexPath:indexPath];
        cell.textLabel.text = self.content[indexPath.row];
    
        return cell;
    }
    

    Good Luck!

    Also, this is my first Answer/Question combo, so I really have no idea how to approach it, so if i've done anything wrong, please tell me and/or change it. Thanks!

提交回复
热议问题