ios - 为什么单元格不随其内容调整大小?

标签 ios swift uitableview resize

我已经阅读了我可以在这里找到的所有类似问题,并应用了所有建议的解决方案……它们都不起作用。

TableView 是一个分组 View ,textLabeldetailTextLabel 的行数在代码和 Storyboard 中都设置为 0,字符串数据由通过 "\n" 字符创建的多行字符串组成...

tableView.estimatedRowHeight 设置为其他值而不是自动设置 43.5,然后按照其他一些问题中的建议将 tableView.rowHeight = UITableView.automaticDimension 完全没有任何作用...或者如果我放 100 之类的东西,它会调整所有单元格的大小,而不仅仅是那些有多行文本的单元格。

我不熟悉 TableView 中的这种类型的行为:根据我的经验,只需将行数设置为 0 即可使标签正常工作...

为什么我会得到这个结果?

shrank image

编辑:我也尝试实现 heightForRowAt 方法,传递所需的部分和行,但没有任何改变......我迷路了。

编辑 (2):删除了之前的代码片段并插入了 ViewController 源代码:我唯一可以展示的与单元格相关的是这个,因为唯一的属性是填充表格 View 的自定义数据类型的实例(它在这方面非常擅长)并且 viewDidLoad() 没有在其中添加我的任何内容。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.numberOfLines = 0
        cell.detailTextLabel?.numberOfLines = 0

        func configureTextLabels(withString labelText: String, and detailText: String) {
            cell.textLabel?.text = labelText
            cell.detailTextLabel?.text = detailText
        }

        if let country = detailCountry {
            switch indexPath.section {
            // insert 14 different cases here
            case 14: 
                func populateRows() {
                    let regionalBlocs = country.regionalBlocs

                    for i in 0 ..< regionalBlocs.count {
                        switch indexPath.row {
                        case 0 + (i * 4): configureTextLabels(withString: "Name: ", and: "\(regionalBlocs[i].name)")
                        case 1 + (i * 4): configureTextLabels(withString: "\tAcronym: ", and: "\(regionalBlocs[i].acronym)")
                        case 2 + (i * 4):
                            var detailText = ""
                            let otherNames = regionalBlocs[i].otherNames

                            if !otherNames.isEmpty {
                                for name in otherNames {
                                    if name == otherNames.last {
                                        detailText += name
                                    } else {
                                        detailText += "\(name)\n"
                                    }
                                }
                                configureTextLabels(withString: "\tOther names: ", and: detailText)
                            } else {
                                configureTextLabels(withString: "", and: "No other names found")
                            }
                        case 3 + (i * 4):
                            var detailText = ""
                            let otherAcronyms = regionalBlocs[0].otherAcronyms

                            if !otherAcronyms.isEmpty {
                                for acronym in otherAcronyms {
                                    if acronym == otherAcronyms.last {
                                        detailText += acronym
                                    } else {
                                        detailText += "\(acronym)\n"
                                    }
                                }
                                configureTextLabels(withString: "\tOther acronyms: ", and: detailText)
                            } else {
                                configureTextLabels(withString: "", and: "No other acronym found")
                            }
                        default: break
                        }
                    }
                }

                let regionalBlocs = country.regionalBlocs

                if regionalBlocs.isEmpty {
                    cell.textLabel?.text = ""
                    cell.detailTextLabel?.text = "No regional bloc data found for this country"
                } else {
                    populateRows()
                }
            }
        return cell
        }

最佳答案

你可以在这里查看答案:Add multiple lines to detailTextLabel in UITableViewCell

但我建议您为这种情况创建自定义单元格,您的问题是 UIKit 的错误。

关于ios - 为什么单元格不随其内容调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57763641/

相关文章:

ios - 在 UITableView 中仅重新加载一个部分标题

ios - 索引 1 超出范围 [0 .. 0] 向下滚动时崩溃

iphone - 在保持 UI 响应的同时向核心数据添加大量数据

ios - 如何使父类(super class)的自定义 init 方法仅对其子类内部可见?

ios - 是不是JSON解析错了?

ios - 在 tableView :editActionsForRowAt: 中处理图像

ios - UICollectionView 中的多种单元格类型

ios - 我在哪里放置 iOS 8 + Swift 中的常用实用程序函数

swift - 无法连接 socket

swift - 通过 viewDidAppear() 或 viewWillLayoutSubviews() 设置圆角半径?