开发者

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.)


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


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


I've noticed this problem as well. My workaround has been to just make my table cells smaller (300 width instead of 320). It's not a great solution, but it works well.

I don't believe you can get rid of the table view insets on an individual basis when in "grouped" mode. I could be wrong though!


What problem are you having? In drawRect you are given a rect, and know your total size - just conform to that space. If you are using layoutSubviews, same thing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜