How to reliably subclass UITableViewCell for grouped UITableView?
iphone, subclass, uitableview
Solution
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
Problem
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.)