iOS11中表格视图的分组有些问题 - UITableViewAutomaticDimension的问题

3
我有一个带可折叠部分的tableView。在iOS 10或更早版本中,它可以完美地工作。但在iOS 11中,tableView的各个部分会创建浮动的区域标题,并在其他部分中间重复出现区域标题,如下所示:

enter image description here

我是一名有用的助手,可以为您翻译文本。

我一直在研究这个问题,这个问题已经在其他地方报道并讨论了在这里

这是我的tableView代码:

class CollapsibleTableViewController: UITableViewController {

    var sections = sectionsData

    override func viewDidLoad() {
        super.viewDidLoad()
        let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)


          // Auto resizing the height of the cell
        tableView.estimatedRowHeight = 44.0
        tableView.rowHeight = UITableViewAutomaticDimension

        self.title = "Education"
                for index in 0 ..< sections.count {
                        sections[index].collapsed = true
                }
    }
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44.0
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 1.0
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension   //making this a fixed value fixes the drawing bug but doesn't let the cells autosize
}

在我之前发布的另一个相关问题中,回答者建议预设估计高度如下:
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

这个修复了重复的部分,但是缩小了我的tableView单元格,使标签字段不再可见,而且单元格不能自动调整大小以显示其中所有的数据。
有报道称iOS 11改变了tableView高度的工作方式,采用了估算值。
我也尝试使用以下所有字段的估算值:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.sectionHeaderHeight = UITableViewAutomaticDimension          
tableView.sectionFooterHeight = UITableViewAutomaticDimension

似乎没有一种方法可以既允许单元格正确自适应大小又防止单元格与浮动区段标题重复。

有什么想法可以解决这个问题吗?


你能把这个项目上传到GitHub上吗?这样我就可以看一下了。我有一个小时的空闲时间,想要理解并可能修复它。 - Alex Zavatone
在iOS 10上,您的表格将使用自动调整大小的单元格和固定大小的标题和页脚。在iOS 11上,您的表格使用自动调整大小的单元格、标题和页脚。尝试将“estimatedSectionHeaderHeight”和“estimatedSectionFooterHeight”设置为零,以返回到固定大小的标题和页脚。保持其他所有内容不变。 - Darren
1
@Darren - 我尝试了那个方法,但很遗憾没有帮助。仍然出现重复的标题部分。 - LearningSwift
1个回答

3
viewWillAppear()中编写您的tableView初始值,然后按如下方式提供UItableViewAutomaticDimension: tableView.estimatedRowHeight = 75 tableView.rowHeight = UITableViewAutomaticDimension 如有必要,请重复此步骤以获取标题、页脚等。

1
是的!谢谢!将这个放到viewWillAppear而不是viewDidLoad似乎解决了问题。 - LearningSwift

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接