0

可能重复:
如何从 Xib 文件加载自定义 UITableViewCells?

这是我的代码..

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"LibraryListingCell";

    DVDListingViewCell *cell = (DVDListingViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"DVDListingView" owner:self options:nil];
        cell = [_cell autorelease];
        _cell = nil;
    }

    cell.titleLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];    
    cell.featureLengthLabel.text = [NSString stringWithFormat:@"%@ minutes", 
                                    [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"featureLength"]];
    cell.coverImageView.image = [UIImage 
                                 imageNamed:[[dao libraryItemAtIndex:indexPath.row] valueForKey:@"coverImage"]];

    return cell;
}

如何解决 Thread1 : Signal SIGABRT 问题与这一行 [[NSBundle mainBundle] loadNibNamed:@"DVDListingView" owner:self options:nil];

4

2 回答 2

1

我建议您尝试一下,同时确保您的“DVDListingView.xib”是自定义单元格的 NIB

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"LibraryListingCell";

    DVDListingViewCell *cell = (DVDListingViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DVDListingView" owner:self options:nil];
        for(id currentObject in topLevelObjects){
            if([currentObject isKindOfClass:[DVDListingViewCell class]]){
                cell = (DVDListingViewCell *)currentObject;
                break;
            }
        }
    }

    cell.titleLabel.text = [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"title"];    
    cell.featureLengthLabel.text = [NSString stringWithFormat:@"%@ minutes", 
                                    [[dao libraryItemAtIndex:indexPath.row] valueForKey:@"featureLength"]];
    cell.coverImageView.image = [UIImage 
                                 imageNamed:[[dao libraryItemAtIndex:indexPath.row] valueForKey:@"coverImage"]];

    return cell;
}
于 2012-10-01T19:16:16.277 回答
0

线

 [[NSBundle mainBundle] loadNibNamed:@"DVDListingView" owner:self options:nil];

返回一个 NSArray。它需要是这样的:

NSArray *xibObjectArray =  [[NSBundle mainBundle] loadNibNamed:@"DVDListingView" owner:self options:nil];
//retrieve items from the array
if (xibObjectArray.count == 1) {
     cell = [xibObjectArray objectAtIndex:0];
} else {
     for (UIView *randomView in xibObjectArray) {
        if ([randomView isKindOfClass:[DVDListingViewCell class]]) {
           cell = (DVDListingViewCell *)randomView;
        }
     }
}
于 2012-10-01T20:49:55.320 回答