通过xib加载视图到控制器时 出现的各种错误

1.Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]

原因:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
返回的为空。
我这里的问题是在此方法中加载自定义cell时出得问题,加载xib未成功。
解决:如问题2.

2.关于xcode TableView使用Nib文件出现Assertion

原因:我在使用tableViewCell的nib文件时 出现此问题

解决:自定义cell  xib方式

 

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

{

    

    static NSString *CellIdentifier = @"MyCell";

    BOOL nibsRegistered = NO;

    if (!nibsRegistered) {

        UINib *nib = [UINib nibWithNibName:NSStringFromClass([MyCell class]) bundle:nil];

        [tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];

        nibsRegistered = YES;

    }

    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        return cell;

}

 
3.怎样加载xib
 
  1、self.view = [[[NSBundle mainBundle] loadNibName:@"mianview"  owner:self  options:nil] lastObject];
 
  2、UIViewController *controller = [[UIViewController alloc] initWithNibName:@"mainview" bundle:nil];
      self.view = controller.view;
 
4.自定义的View  customer  选择view时,imageView连线报错this class is not key value coding-compliant for the key head.'
 解决:view对应customer选择相应的View,File's owner对应customer选择相应的Controller类
 
5.  Terminating app due to uncaught exception 'NSUnknownKeyException'
原因:我这里在xib中有多余的控件 ,删掉就可以了。一般此问题 都是xib中连接问题。
 
原文地址:https://www.cnblogs.com/hanyutong/p/4475661.html