【发布时间】:2012-03-12 08:41:39
【问题描述】:
在编写UITableViewCell 的自定义子类时,我发现结果对于普通样式的UITableView 的矩形单元格效果很好,但对于分组样式表中的圆形单元格则根本不起作用。
有没有办法可靠地继承 UITableViewCell 来绘制适用于分组样式表的单元格? (不使用 Interface Builder。)
【问题讨论】:
标签: iphone uitableview subclass
在编写UITableViewCell 的自定义子类时,我发现结果对于普通样式的UITableView 的矩形单元格效果很好,但对于分组样式表中的圆形单元格则根本不起作用。
有没有办法可靠地继承 UITableViewCell 来绘制适用于分组样式表的单元格? (不使用 Interface Builder。)
【问题讨论】:
标签: iphone uitableview subclass
答案是否可以像在 UITableViewCell 子类的 layoutSubviews 方法中首先调用 [super layoutSubviews] 一样简单?
这是我的代码。
首先我创建UITextField 并将其添加到initWithStyle: 方法中的contentView:
- (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;
}
然后在 layoutSubviews 中,我得到了这个:
-(void)layoutSubviews
{
inputField.frame = CGRectMake(5, 5, 100, 20);
}
使用该代码,文本字段距离屏幕左侧 5px,当然,当处于分组模式时,也就是表格单元格左侧 5px。换句话说,在表格视图单元格之外。不好。
使用此代码,inputField 被放置在单元格右侧 5px 处,就像我希望的那样:
-(void)layoutSubviews
{
[super layoutSubviews]; // the magic line
inputField.frame = CGRectMake(5, 5, 100, 20);
}
不过,我可能完全误解了你遇到的问题!
埃里克
【讨论】:
我曾经对UITableViewCell 子类有很多问题,但后来我停止了子类化。
在UITableViewCell 的contentView 属性中添加子视图似乎在我遇到的任何情况下都能完成同样的事情,所以我只是在我的UITableViewController 中这样做。
这是一个有标题和值的例子:
- (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;
}
【讨论】:
configureWithDataObject: (id)dataObject 方法的子类,然后我将其子类化为我的自定义单元格。我的视图控制器自动选择正确的单元格子类并调用configureWithDataObject:。使开发变得快速而简单(尽管从技术上讲,它的效率不如在静态代码中完成)。
我也注意到了这个问题。我的解决方法是让我的表格单元格更小(300 宽度而不是 320)。这不是一个很好的解决方案,但效果很好。
我不相信您可以在“分组”模式下单独删除表格视图插图。不过我可能是错的!
【讨论】:
你有什么问题?在 drawRect 中,您将获得一个矩形,并且知道您的总大小 - 只需符合该空间即可。如果你使用 layoutSubviews,同样的事情。
【讨论】: