自定义 UITableViewCell:“loadNibNamed:"上的 SIGABRT [英] Custom UITableViewCell: SIGABRT on "loadNibNamed:"

查看:19
本文介绍了自定义 UITableViewCell:“loadNibNamed:"上的 SIGABRT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义 nib 文件来处理 UITableViewCell 的子类.

I'm trying to get the hang of doing a subclass of UITableViewCell with a custom nib file.

子类的名称是 MyCustomCell..xib 文件只有一个 UITableViewCell 和一个 UILabelcellTitle",我已经将它连接到 UITableViewCell 子类中的一个插座.

The subclass's name is MyCustomCell. The .xib file just has a UITableViewCell with a single UILabel "cellTitle" and I've connected it to an outlet in the UITableViewCell subclass.

在返回单元格的 MyCustomCell 类中,我在以下行中收到 SIGABRT 错误:

In the class in MyCustomCell that returns the cell, I get a SIGABRT error on the following line:

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

这里是 cellForRowAtIndexPath 的实现:

Here's the implementation of 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 的实现:

Here's the implementation for 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 错误

I get the SIGABRT error in the line

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

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

in the above method. What am I doing wrong here?

推荐答案

除了@Seega 关于你的笔尖名称的评论似乎合理之外,你在笔尖加载代码中存在许多问题;

Apart from the comment about your nib name from @Seega which seems reasonable you have numerous issues in the nib loading code;

+ (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 实例为 nil,因此向它发送 class 方法是空操作并返回 nil.在那种情况下 isKindOfClass: 不会返回你的想法.

Your customCell instances is nil so sending it the class method is a no-op and returns nil. In that case isKindOfClass: is not going to return what you think.

另外,不要遍历从 loadNibNamed:owner:options: 方法返回的对象列表.而是在文件所有者和 nib 文件中的单元格之间创建连接,以便在 nib 加载时设置它.然后将您的代码更改为如下所示;

Also, don't iterate through the list of objects returned from the loadNibNamed:owner:options: method. Instead create a connection between the file's owner and the cell in the nib file so that it gets set when the nib loads. Then change your code to look like this;

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

此外,用小写字符命名类是不典型的.其他人阅读你的代码会认为这是一个变量而不是一个类.改为将其称为 MyCustomCell.

Also, it is atypical to name a class with a lowercase character. Others reading your code will think that is a variable and not a class. Call it MyCustomCell instead.

这篇关于自定义 UITableViewCell:“loadNibNamed:"上的 SIGABRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆