iOS 8中的自定义(动态高度)单元格 - 可能没有自定义UITableViewCell?

SAH*_*AHM 8 row-height uitableview dynamic-text autolayout ios8

是否可以在不创建自定义UITableViewCell的情况下在iOS 8中自行调整UITableViewCell的大小?

我认为标准的UITableViewCell类型(UITableViewCellStyleDefault,UITableViewCellStyleSubtitle,UITableViewCellStyleValue1,UITableViewCellStyleValue2)内置了自动布局约束.事实证明,在Storyboard中无法更改非自定义单元格的约束.

但是当我使用UITableViewCellStyleValue1类型的非自定义单元格时,在Storyboard中设置它,将textOabel和detailTextLabel的numberOfRows设置为0,并将viewDidLoad代码设置为如下所示,在自动调整中仅考虑单元格的textLabel.细胞的高度.如果detailTextLabel最终显示在比textLabel多的行上,则detailTextLabel的文本会溢出单元格的顶部和底部边缘.同样,单元格为textLabel正确调整大小,但似乎在其大小调整过程中忽略detailTextLabel.

我需要知道的主要事情是 - 如果我想要正确支持动态文本和自我调整大小,我是否需要为可以使用标准单元格的行创建自定义单元格?

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEstimatedRowHeight:DEFAULT_ROW_HEIGHT];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];
}
Run Code Online (Sandbox Code Playgroud)

Tra*_* M. 4

我刚刚在 iOS 10/XCode 8 中使用不同的单元格类型尝试过此操作(在 iOS 9/XCode 7 中结果相同),看起来仅适用于 textLabel 而不适用于细节文本标签。

(基本上重复OP提到的问题)

ViewController 代码在detailTextLabel 和textLabel 上交替设置一些文本。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        if indexPath.row % 2 == 0 {
            cell.textLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            cell.detailTextLabel?.text = "<- textLabel"
        } else {
            cell.textLabel?.text = "detailTextLabel ->"
            cell.detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

}
Run Code Online (Sandbox Code Playgroud)

确保将 textLabel 和 textDetailLabel 的 line 属性设置为 0,以下是结果。

基本细胞 在此输入图像描述

右侧细节单元 在此输入图像描述

左侧细节单元 在此输入图像描述

字幕单元 在此输入图像描述

我会将其报告为错误。