简体   繁体   中英

UITableView error “Could not load NIB in bundle:” for custom cell

When I try with the following coding for custom cell, Its showing error.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpletablcellCell";
    SimpletablcellCell *cell = (SimpletablcellCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpletablcellCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [ar objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[ar1 objectAtIndex:indexPath.row]];
    cell.prepTimeLabel.text = [ar objectAtIndex:indexPath.row];

    return cell;
}

Error msg

'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/apple/Library/Application Support/iPhone Simulator/6.0/Applications/FE02B3A1-C40C-4FAC-BBA6-B245350F3754/SImpleTab.app> (loaded)' with name 'SimpletablcellCell''
 *** First throw call stack:
(0x1c8f012 0x10cce7e 0x1c8edeb 0x22ffac 0x23198d 0x3418 0xcef4b 0xcf01f 0xb780b 0xc819b 0x6492d 0x10e06b0 0x228bfc0 0x228033c 0x228beaf 0x1038cd 0x4c1a6 0x4acbf 0x4abd9 0x49e34 0x49c6e 0x4aa29 0x4d922 0xf7fec 0x44bc4 0x44dbf 0x44f55 0x4df67 0x2d40 0x117b7 0x11da7 0x12fab 0x24315 0x2524b 0x16cf8 0x1beadf9 0x1beaad0 0x1c04bf5 0x1c04962 0x1c35bb6 0x1c34f44 0x1c34e1b 0x127da 0x1465c 0x29fd 0x2925)
libc++abi.dylib: terminate called throwing an exception

Please check, what is wrong with this code?

Does a .nib / .xib exist in your project named SimpletablcellCell ? You might have a typo in the name, please check carefully.

Make sure the file is part of your target. You can check this by opening the right-side menu in Xcode. Example:

文件目标设置

If the cell exists, is it being copied in the build process? Check the "Copy bundle resources" phase in your project target build settings and make sure it is being copied. If not, add the file manually to this phase.

I had this problem when using Cocoapods for a private pod library of mine.

I had incorrectly included my xib files in s.source_files like so

s.source_files =  'MyLibrary/**/*.{h,m,xib}' //DOES NOT WORK

The solution was to use the resources attribute:

s.resources = 'MyLibrary/**/*.{xib}'        //PROBLEM SOLVED
s.source_files =  'MyLibrary/**/*.{h,m}'

...

Then there is the problem that occurs if your podspec is looking for the xib as specified by s.resources above, but the xib is not in that path.

Once I mistakingly added a xib to my root project directory (where your AppDelegate is) and so Cocoapods would not copy it to any clients using the pod.

I fixed this by either changing the s.resources path or moving the xib so that it would be in the s.resources path.

This error can occur when you rename some files outside XCode. To solve it you can just remove the files from your project (Right Click - Delete and "Remove Reference") You re-import the files in your project and everything will be ok !

If you are registering the nib using:

let nib: UINib = UINib(nibName: "myNib", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "myCell")

make sure it is done in the viewDidLoad() function. I had placed it in the init() function and received this error. Once moved to viewDidLoad() , it worked well.

  1. Make sure your custom cell is added to the target you are building. Register that cell without fail.
  2. There should be no IBOutlet errors in xib. Also, connect the view to the file owner.
  3. The common error could be the name of the nib you are loading is incorrect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM