Ale*_*lds 11 iphone subclass uitableview
在编写自定义子类时UITableViewCell,我发现结果适用于普通样式的矩形单元格UITableView,但对于分组样式表中的圆角单元格则根本不起作用.
有没有办法可靠地子类UITableViewCell绘制适用于分组样式表的单元格?(不使用Interface Builder.)
答案可能是第一个电话一样简单[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;
}
Run Code Online (Sandbox Code Playgroud)
然后在layoutSubviews中,我有这个:
-(void)layoutSubviews
{
inputField.frame = CGRectMake(5, 5, 100, 20);
}
Run Code Online (Sandbox Code Playgroud)
使用该代码,文本字段距屏幕左侧5px,当然,当它处于分组模式时,表格单元格左侧是5px.换句话说,表视图单元格的OUTSIDE.不好.
使用此代码,并将其inputField放置在单元格的右侧5px,就像我想要的那样:
-(void)layoutSubviews
{
[super layoutSubviews]; // the magic line
inputField.frame = CGRectMake(5, 5, 100, 20);
}
Run Code Online (Sandbox Code Playgroud)
不过,我可能完全误解了你遇到的问题!
埃里克
我曾经有很多UITableViewCell子类的问题,但后来我只是停止了子类化.将子视图添加到a的contentView属性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;
}
Run Code Online (Sandbox Code Playgroud)
Ken*_*ner -1
你有什么问题吗?在绘制矩形中,您会得到一个矩形,并知道您的总大小 - 只需符合该空间即可。如果您使用的是layoutSubviews,同样的事情。
| 归档时间: |
|
| 查看次数: |
10826 次 |
| 最近记录: |