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

后端 未结 14 2165
情书的邮戳
情书的邮戳 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:13

    I dragged collection view cell from other project and got this.
    'invalid nib registered for identifier (cell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance' .
    Then i compared the collection view cell from created my me and found reusable view missing. Hope this help.

    0 讨论(0)
  • 2020-12-14 00:15

    I had the same problem! And in my case custom cell was the subclass of UIView by mistake instead of UITableViewCell. So replacing UIView with UITableViewCell fixed the problem!

    0 讨论(0)
  • 2020-12-14 00:15

    This error is mainly due to some extra views that are added by mistake. Have a look in the .xib file and check for extra unwanted view added by mistake. Delete that and it will work perfect. You can check for unwanted views by this is how it looks

    0 讨论(0)
  • 2020-12-14 00:16

    I had this issue, finally found out that i had created UITableViewCell Subclass instead of CollectionViewCell subclass. it was evening, and i was tired,lol. Fixed it in the mng.

    0 讨论(0)
  • 2020-12-14 00:17

    In my case, I have added UITableViewHeaderFooterView subclass and XIB in different target instead of actual target.

    Make sure it is in running target.

    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题