开发者

Return different UITableViewCell types in cellForRowAtIndexPath

I have created a custom UITableViewCell loading it from a xib file following the helpful guidance of; How do you load custom UITableViewCells from xib files.

I only want this custom cell for the first row in my UITableView, the others are simple labels and the standard UITableViewCell is fine.

The problem is when I include the custom cell the whole UITableView isn't shown and the screen is blank except for the navigation elements. Using just the standard cells the table appears fine so it is something to do with the custom cell开发者_开发知识库.

Here is the code I have to return the cells;

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

    if(indexPath.row == 0)  {
        ArticleDetailCell *cell = (ArticleDetailCell *)[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@Detail", reuseIdentifier]];

        if(!cell) {
            // Load the top-level objects from the custom cell XIB.
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ArticleDetailCell" owner:self options:nil];
            // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
            cell = (ArticleDetailCell *)[topLevelObjects objectAtIndex:0];
        }

        [[(ArticleDetailCell *)cell judges] setText:[[self caseBaseArticle] judges]];
        [[(ArticleDetailCell *)cell judgementDate] setText:[[self caseBaseArticle] judgmentDate]];
        [[(ArticleDetailCell *)cell court] setText:[[self caseBaseArticle] court]];

        return cell;

    } else {    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

        if(!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
            [[cell textLabel] setNumberOfLines:1];
            [[cell textLabel] setFont:[UIFont systemFontOfSize:14]];
            [[cell textLabel] setText:articleRowTitles[indexPath.row-1]];
        }

        if ([self tableRowHasContentDetail:indexPath]) {
            [self enableTableCell:&cell];
        }else {
            [self disableTableCell:&cell];
        }       

        return cell;
    }
}

The ArticleDetailCell is loaded from the xib file ok and the properties get set correctly as I can see this in the debugger. Stopping at return cell when I print the custom cell object from (gdb) I get;

<ArticleDetailCell: 0x4bc22b0; baseClass = UITableViewCell; frame = (0 0; 320 367); autoresize = W+TM+BM; layer = <CALayer: 0x4bc23c0>>

Is there some problem with the cell dereferencing? I'll still getting my head around retain counts and when they do and don't increment.

Why does the whole UITableView disappear because of a problem with a single cell?

EDIT: FYI @BoltClock

- (void)disableTableCell:(UITableViewCell **)cell {
    [*cell setAccessoryType:UITableViewCellAccessoryNone];
    [*cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [[*cell textLabel] setTextColor:[UIColor lightGrayColor]];
}


Where is reuseIdentifier defined? Is it always available to this method?


The problem was mostly with the xib file for the custom cell. Strict following of Loading Custom Cells from NIB Files from the Apple Developer Center did the trick.

The key thing was to set the File's Owner in the custom cell to be the type of UITableViewController where I'd use the cell. The UITableViewController also had an IBOutlet property for the custom UITableViewCell which was used to connect the File's Owner to the custom cell in the xib file.

Importantly the Identifer was set in the xib file for the Table View Cell so that it matches the value used in the dequeueReusableCellWithIdentifier method.

The cellForRowAtIndexPath ended up like this where articleDetail is the IBOutlet property;

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

    if(indexPath.row == 0)  {
        ArticleDetailCell  *cell = (ArticleDetailCell *)[tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@Detail",reuseIdentifier]];

        if(!cell) {
            [[NSBundle mainBundle] loadNibNamed:@"ArticleDetailCell" owner:self options:nil];
            cell = articleDetail;
            [self setArticleDetail:nil];
        }

        [[cell judges] setText:[[self caseBaseArticle] judges]];
        [[cell judgementDate] setText:[[self caseBaseArticle] judgmentDate]];
        [[cell court] setText:[[self caseBaseArticle] court]];

        return cell;

    } else {    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

        if(!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease];
            [[cell textLabel] setNumberOfLines:1];
            [[cell textLabel] setFont:[UIFont systemFontOfSize:14]];
            [[cell textLabel] setText:articleRowTitles[indexPath.row-1]];
        }

        if ([self tableRowHasContentDetail:indexPath]) {
            [self enableTableCell:&cell];
        }else {
            [self disableTableCell:&cell];
        }       

        return cell;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜