在UITableViewCell中设置textLabel的宽度

时间:2010-11-01 18:57:55

标签: iphone iphone-sdk-3.0 ios ios4

我有一个UITableViewCell,它有一个textLabel和一个UITextField附加为子视图,如下所示:

cell = [tableView dequeueReusableCellWithIdentifier:Value1CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:Value1CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = @"Name";
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
playerTextField.placeholder = @"John Doe";
playerTextField.textAlignment = UITextAlignmentLeft;
[playerTextField setEnabled: YES];
[cell addSubview:playerTextField];
[playerTextField release];

textLabel占据了很多空间。如何设置textLabel的宽度,以便UITableViewCell的其余部分获得提醒宽度?

谢谢

2 个答案:

答案 0 :(得分:1)

textLabel是只读的。为什么不创建自己的并将其添加到单元格?

答案 1 :(得分:1)

简而言之,您可以使用layoutSubviews方法在UITableViewCell中设置UITextLabel的宽度,该方法在UITableViewCell类中执行。使用它需要您最有可能创建自己的自定义UITableViewCell类,如Joseph Tura所述。

以下链接中的已发布答案有关如何使用layoutSubviews的一个很好的示例: Having a UITextField in a UITableViewCell

此解决方案还概述了另一种方法: How to put a UITextField inside of a UITableViewCell (grouped)?

相关问题