9

Is it possible to self-size a UITableViewCell in iOS 8 without creating a Custom UITableViewCell?

I thought that the standard UITableViewCell types (UITableViewCellStyleDefault, UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, UITableViewCellStyleValue2) had built in auto layout constraints. This is confirmed by the fact that the constraints for non-custom cells cannot be changed in Storyboard.

But when I use a non-custom cell of type UITableViewCellStyleValue1, set it up in Storyboard, set numberOfRows for textLabel and detailTextLabel to 0, and set the viewDidLoad code as below, only the textLabel of the cell is accounted for in the autosizing of the height of the cell. If the detailTextLabel ends up displaying on more lines than the textLabel, the text for detailTextLabel spills out over the top and bottom edges of the cell. Again, the cell does resize properly for the textLabel, but seems to ignore the detailTextLabel in its resizing process.

The main thing I need to know is - do I need to create a custom cell even for the rows that can use a standard cell if I want to properly support Dynamic Text and Self-Sizing?

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEstimatedRowHeight:DEFAULT_ROW_HEIGHT];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];
}
4

1 回答 1

4

我刚刚在 iOS 10/XCode 8(iOS 9/XCode 7 中的结果相同)中使用不同的单元格类型进行了尝试,看起来它只适用于 textLabel 而不是 detailTextLabel。

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

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

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
    }

}

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

基本单元 在此处输入图像描述

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

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

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

我会将此报告为错误。

于 2016-07-18T21:56:06.470 回答