简体   繁体   English

自定义单元的UITableView错误“无法在捆绑包中加载NIB:”

[英]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 ? 您的名为SimpletablcellCell的项目中是否存在.nib / 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. 您可以通过在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. 将Cocoapods用于我的私人豆荚库时遇到了这个问题。

I had incorrectly included my xib files in s.source_files like so 我将xib文件错误地包含在s.source_files中,就像这样

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. 如果您的podspec正在按上面的s.resources指定的方式查找xib,但是xib不在该路径中,则会出现问题。

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. 一旦我错误地将xib添加到我的根项目目录(您的AppDelegate所在的目录)中,因此Cocoapods不会将其复制到使用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. 我通过更改s.resources路径或移动xib使其固定在s.resources路径中来解决此问题。

This error can occur when you rename some files outside XCode. 当您重命名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. 确保在viewDidLoad()函数中完成此操作。 I had placed it in the init() function and received this error. 我将其放在init()函数中并收到此错误。 Once moved to viewDidLoad() , it worked well. 一旦移至viewDidLoad() ,它就会很好地工作。

  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. xib中应该没有IBOutlet错误。 Also, connect the view to the file owner. 另外,将视图连接到文件所有者。
  3. The common error could be the name of the nib you are loading is incorrect. 常见的错误可能是您正在加载的笔尖的名称不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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