开发者

Custom UITableViewCell: SIGABRT on "loadNibNamed:"

开发者 https://www.devze.com 2023-03-24 13:20 出处:网络
I\'m trying to g开发者_运维技巧et the hang of doing a subclass of UITableViewCell with a custom nib file.

I'm trying to g开发者_运维技巧et the hang of doing a subclass of UITableViewCell with a custom nib file.

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.

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];

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;
}

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;
}

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?


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;
}

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.

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;
}

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.


i think you should use the string parameter "nibName" instead of @"View"

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


There were a number of problems in my code as people have pointed out. The issue that was causing the actual problem I was talking about in the title turned out to be an issue in IB: I had outlets referencing file's owner rather than the actual objects.

But I'm giving this one to Bill because he addressed the largest number of mistakes...

0

精彩评论

暂无评论...
验证码 换一张
取 消