iOS 8中的自调整大小(动态高度)单元格-是否可以不使用自定义UITableViewCell?

iOS 8中的自调整大小(动态高度)单元格-是否可以不使用自定义UITableViewCell?,uitableview,ios8,autolayout,row-height,dynamic-text,Uitableview,Ios8,Autolayout,Row Height,Dynamic Text,在iOS 8中,是否可以在不创建自定义UITableViewCell的情况下自行调整UITableViewCell的大小 我认为标准的UITableViewCell类型(UITableViewCellStyleDefault、UITableViewCellStyleSubtitle、UITableViewCellStyleValue1、UITableViewCellStyleValue2)内置了自动布局约束。这一点可以通过以下事实得到证实:不能在故事板中更改非自定义单元的约束 但是,当我使用UI

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

我认为标准的UITableViewCell类型(UITableViewCellStyleDefault、UITableViewCellStyleSubtitle、UITableViewCellStyleValue1、UITableViewCellStyleValue2)内置了自动布局约束。这一点可以通过以下事实得到证实:不能在故事板中更改非自定义单元的约束

但是,当我使用UITableViewCellStyleValue1类型的非自定义单元格,在故事板中设置它,将textLabel和detailTextLabel的numberOfRows设置为0,并按如下方式设置viewDidLoad代码时,在自动调整单元格高度时,只考虑单元格的textLabel。如果detailTextLabel最终显示的行数多于textLabel,则detailTextLabel的文本将溢出单元格的上边缘和下边缘。同样,单元格确实可以正确调整textLabel的大小,但在调整大小的过程中似乎忽略了detailTextLabel

我需要知道的主要问题是,如果我想正确地支持动态文本和自调整大小,我是否需要创建一个自定义单元格(即使是可以使用标准单元格的行)

- (void)viewDidLoad {

    [super viewDidLoad];

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

我刚刚在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
    }

}
类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource{
@IBVAR表格视图:UITableView!
重写func viewDidLoad(){
super.viewDidLoad()
tableView.EstimateDrowEight=44
tableView.rowHeight=UITableViewAutomaticDimension
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(带有标识符:“cell”,用于:indexath)
如果indexath.row%2==0{
cell.textLabel?.text=“Lorem ipsum door sit amet,concetetur adipiscing elit,sed do eiusmod temporal incidedut ut laboure and dolore magna aliqua。”
cell.detailTextLabel?.text=“”
cell.detailtextlab?.text=“Lorem ipsum door sit amet,concetetetur adipiscing elit,sed do eiusmod temporal incident ut laboure and dolore magna aliqua。”
}
返回单元
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回10
}
}
请确保将textLabel和textDetailLabel的line属性设置为0,结果如下

基本细胞

右细节单元

左细节单元

字幕单元


我将报告这是一个bug。

我发现的所有内容都表明您必须使用-tableView:heightforrowatinexpath:或自定义子类。我不明白为什么默认单元类中的autolayout不能正确设置为iOS 8部署,但看起来不是这样。@Thromordyn在iOS 9中有任何改变吗?尝试重新创建右侧细节单元格的所有方面,只是为了在detailTextLabel长于1行时增加行高。这在什么地方做过吗?尝试重新创建轮子,但是有太多的事情要考虑,这感觉像是在浪费时间。在这个问题上浪费了几个小时--@aehlke仍然面临着围绕这个问题的问题。本希望iOS 9中会有一个修复程序。您是否为UITableViewCellStyleValue1和详细文本标签尝试过此操作?您是对的,它看起来只对textLabel有效,而对detailTextLabel无效。如果未将约束正确添加到detailTextLabel,则这一定是一个问题。将更新我的答案。