Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reliably subclass UITableViewCell for grouped UITableView?

When writing a customized subclass of UITableViewCell, I find that the results work well for the rectangular cells of a plain-styled UITableView, but do not work at all for the rounded cells in a grouped-styled table.

Is there a way to reliably subclass UITableViewCell to draw cells which work for grouped-style tables? (Without using Interface Builder.)

like image 744
Alex Reynolds Avatar asked Oct 17 '09 11:10

Alex Reynolds


2 Answers

Could the answer be as simple as first calling [super layoutSubviews] inside your UITableViewCell subclass’s layoutSubviews method?

Here is my code.

First I create the UITextField and add it to the contentView in the initWithStyle: method:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        inputField = [[UITextField alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:inputField];
        inputField.borderStyle = UITextBorderStyleLine;
        [inputField release];
    }
    return self;
}

Then in layoutSubviews, I’ve got this:

-(void)layoutSubviews
{
    inputField.frame = CGRectMake(5, 5, 100, 20);
}

With that code, the text field is 5px from the left of the screen, which is, of course, 5px to the left of the table cell when it’s in grouped mode. In other words, OUTSIDE of the table view cell. No good.

Use this code and the inputField is placed 5px to the right of the cell, like I want it to be:

-(void)layoutSubviews
{
    [super layoutSubviews]; // the magic line
    inputField.frame = CGRectMake(5, 5, 100, 20);
}

I could have totally misunderstood the problem you were having, though!

Erik

like image 133
Erik P. Hansen Avatar answered Nov 16 '22 09:11

Erik P. Hansen


I used to have lots of problems with UITableViewCell subclasses, but then I just stopped subclassing. Adding subviews to the contentView property of a UITableViewCell seems to accomplish the same thing in any instance that I've run across, so I just do that inside my UITableViewController.

Here's an example that has a title and value:

- (UITableViewCell *)tableView:(UITableView*)tableView 
         cellForRowAtIndexPath: (NSIndexPath*)indexPath 
{    
    static NSString* CellIdentifier = @"AccountDetailsCell";
    UILabel* mainLabel = nil;
    UILabel* valueLabel = nil;
    const CGFloat kAccountDetailFontSize = 14.0;

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if ( cell == nil ) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault 
                                       reuseIdentifier: CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake( 10.0, 0.0, 150.0, 44.0 )] autorelease];
        mainLabel.tag = MAINLABEL_TAG;
        mainLabel.font = [UIFont boldSystemFontOfSize: kAccountDetailFontSize];
        mainLabel.textAlignment = UITextAlignmentLeft;
        mainLabel.textColor = [UIColor darkGrayColor];
        mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
        mainLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: mainLabel];

        valueLabel = [[[UILabel alloc] initWithFrame: CGRectMake( 150.0, 0.0, 150.0, 44.0 )] autorelease];
        valueLabel.tag = VALUELABEL_TAG;
        valueLabel.font = [UIFont boldSystemFontOfSize: kAccountDetailFontSize];
        valueLabel.textAlignment = UITextAlignmentRight;
        valueLabel.textColor = [UIColor darkTextColor];
        valueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        valueLabel.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: valueLabel];
    }
    else
    {
        mainLabel = (UILabel*)[cell.contentView viewWithTag: MAINLABEL_TAG];
        valueLabel = (UILabel*)[cell.contentView viewWithTag: VALUELABEL_TAG];
    }

    mainLabel.text = (NSString*)kCellTitles[indexPath.section][indexPath.row];
    valueLabel.text = [self tableView: tableView valueLabelTextForRowAtIndexPath: indexPath];

    return cell;
}
like image 8
jessecurry Avatar answered Nov 16 '22 11:11

jessecurry