【问题标题】:Setting the width of textLabel in a UITableViewCell在 UITableViewCell 中设置 textLabel 的宽度
【发布时间】:2010-11-01 18:57:54
【问题描述】:

我有一个 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 的其余部分获得提醒宽度?

谢谢

【问题讨论】:

    标签: iphone iphone-sdk-3.0 ios ios4


    【解决方案1】:

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

    【讨论】:

    • 好吧,实际上我可以将添加的子视图的位置设置为更“左”的位置,它可以工作:)
    【解决方案2】:

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

    以下链接中发布的答案有一个很好的例子来说明如何使用 layoutSubviews: Having a UITextField in a UITableViewCell

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

    【讨论】:

      猜你喜欢
      • 2015-12-18
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 2015-06-09
      • 2020-08-29
      • 2011-06-21
      相关资源
      最近更新 更多