在编写 的自定义子类时UITableViewCell
,我发现结果对于普通样式的矩形单元格效果很好UITableView
,但对于分组样式表中的圆形单元格根本不起作用。
有没有办法可靠地子类UITableViewCell
化以绘制适用于分组样式表的单元格?(不使用界面生成器。)
在编写 的自定义子类时UITableViewCell
,我发现结果对于普通样式的矩形单元格效果很好UITableView
,但对于分组样式表中的圆形单元格根本不起作用。
有没有办法可靠地子类UITableViewCell
化以绘制适用于分组样式表的单元格?(不使用界面生成器。)
答案可以像第一次[super layoutSubviews]
在UITableViewCell
子类的layoutSubviews
方法中调用一样简单吗?
这是我的代码。
首先,我创建UITextField
并将其添加到contentView
方法中initWithStyle:
:
- (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
子类方面遇到很多问题,但后来我停止了子类化。在我遇到的任何情况下,将子视图添加到contentView
a 的属性似乎都可以完成相同的事情,所以我只是在我的.UITableViewCell
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;
}
我也注意到了这个问题。我的解决方法是让我的表格单元格更小(300 宽度而不是 320)。这不是一个很好的解决方案,但效果很好。
我不相信您可以在“分组”模式下单独删除表格视图插图。不过我可能是错的!
你有什么问题?在 drawRect 中,你得到一个矩形,并且知道你的总大小——只要符合那个空间。如果你使用 layoutSubviews,同样的事情。