iOS 8中的自我调整大小(动态高度)单元 – 可能没有自定义UITableViewCell?

是否可以在iOS 8中自行调整UITableViewCell的大小而无需创建自定义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];
}

解决方法

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

(基本上重复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,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,这是结果.

基本细胞

右细节细胞

左细节细胞

字幕细胞

我会将此报告为错误.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...