【问题标题】:textLabel and detailTextLabel aligned to righttextLabel 和 detailTextLabel 右对齐
【发布时间】:2012-02-26 09:09:30
【问题描述】:

我有这个代码:

cell.textLabel.text = [arrBugs objectAtIndex:indexPath.row];
cell.detailTextLabel.text = @"just a text";
cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]];
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.detailTextLabel.textAlignment = UITextAlignmentRight;

cell.detailTextLabel.text 不存在时,textLabel 向右对齐没有任何问题,但是一旦我添加了cell.detailTextLabel.text,它们都会向左对齐。我该如何解决这个问题?我希望它们都向右对齐。

我不使用自定义表格视图单元格。

【问题讨论】:

  • 你怎么知道它在单元格中没有文本的情况下工作?
  • 当我只写 cell.textLabel.text = @"text"; cell.textLabel.textAlignment = UITextAlignmentRight;有用。但是当我添加 cell.detailTextLabel.text = @"just a text"; cell.detailTextLabel.textAlignment = UITextAlignmentRight;突然,单元格向左对齐而不是向右对齐..
  • UITextAlignment 已弃用。使用 NSTextAlignment。

标签: iphone objective-c ios uitableview


【解决方案1】:

对于此类问题,了解您的单元的设置方式很重要: 当您在情节提要编辑器中创建单元格时,您无法稍后在代码中更改所有设置。 只有在代码中创建单元格时,您才能更改所有设置。

但是请回复您的单元格在创建时的设置。

完成了一个简单的项目,单元格具有“正确的细节”样式,我只使用两个文本分配没有问题。

您可能正在尝试更改每个单元格的对齐方式。

【讨论】:

  • 对不起,我没听懂你在说什么。是否可以将 textLabel 和 detailTextLabel 都对齐到单元格的右侧?
  • 嗨,Yossi,没问题。您可以这样做,但只能在自定义单元格中,当您使用预定义的单元格并且想要更改太多时,它将不起作用。如果您只想使主要和细节正确对齐,我可以向您发送一个简单的示例解决方案。你需要这个吗?
  • 嗨 Yossi,我无法在此处放置 zip 文件,因此请转到 link 那里您可以下载快照并查看部分源代码。
【解决方案2】:

您也可以通过继承 UITableViewCell 来创建自己的 UITableViewCell,然后创建自己的 xib 文件并将标签放置在您希望它们拥有的位置。 创建行时,您需要加载新创建的 xib 文件。 这是一个教程:http://www.highoncoding.com/Articles/823_Creating_a_Custom_UITableViewCell.aspx

【讨论】:

    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多