自定义UITableViewCell:“loadNibNamed:”上的SIGABRT

我试图得到一个自定义nib文件的UITableViewCell的子类的挂钩。

子类的名称是MyCustomCell。 .xib文件只有一个具有单个UILabel“cellTitle”的UITableViewCell,并且已经将它连接到UITableViewCell子类的出口。

在MyCustomCell中返回单元格的类中,我在下面一行中得到一个SIGABRT错误:

NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:NULL]; 

这里是cellForRowAtIndexPath的实现:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *kCustomCellID = @"MyCellID"; myCustomCell *cell = (myCustomCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID]; if (cell == nil) { cell = (myCustomCell *)[myCustomCell cellFromNibNamed:@"myCustomCell"]; } //TODO: Configure Cell return cell; } 

以下是myCustomCell cellFromNibNamed的实现:

 + (myCustomCell *)cellFromNibNamed:(NSString *)nibName { NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:NULL]; NSEnumerator *nibEnumerator = [nibContents objectEnumerator]; myCustomCell *customCell = nil; NSObject* nibItem = nil; while ((nibItem = [nibEnumerator nextObject]) != nil) { if ([nibItem isKindOfClass:[myCustomCell class]]) { customCell = (myCustomCell *)nibItem; break; // we have a winner } } return customCell; } 

我得到了行中的SIGABRT错误

  NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:NULL]; 

在上面的方法中。 我在这里做错了什么?

除了关于@Seega的你的笔尖名字的评论,这似乎是合理的,你在笔尖加载代码中有许多问题;

 + (myCustomCell *)cellFromNibNamed:(NSString *)nibName { NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:NULL]; NSEnumerator *nibEnumerator = [nibContents objectEnumerator]; myCustomCell *customCell = nil; NSObject* nibItem = nil; while ((nibItem = [nibEnumerator nextObject]) != nil) { if ([nibItem isKindOfClass:[myCustomCell class]]) { customCell = (myCustomCell *)nibItem; break; // we have a winner } } return customCell; } 

您的customCell实例是零,所以发送它的class方法是一个无操作并返回nil。 在这种情况下, isKindOfClass:不会返回你的想法。

另外,不要迭代loadNibNamed:owner:options:方法返回的对象列表。 相反,在文件的所有者和nib文件中的单元之间创build一个连接,以便在nib加载时设置它。 然后改变你的代码看起来像这样;

 + (myCustomCell *)cellFromNibNamed:(NSString *)nibName { [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL]; myCustomCell *gak = self.theFreshlyLoadedCustomCellThingSetUpInIB; // clean up self.theFreshlyLoadedCustomCellThingSetUpInIB = nil; return gak; } 

而且,用小写字符命名类是非典型的。 其他人阅读你的代码会认为这是一个variables,而不是一个类。 改为调用它MyCustomCell

我想你应该使用string参数“nibName”而不是@“查看”

 NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL]; 

正如人们指出的那样,我的代码中存在一些问题。 导致我在标题中谈到的实际问题的问题竟然是IB中的一个问题:我有sockets引用文件的所有者,而不是实际的对象。

但是我把这个给比尔,因为他解决了最多的错误。