gpt4 book ai didi

swift - 如何遍历所有 UITableView 部分?

转载 作者:搜寻专家 更新时间:2023-11-01 07:04:39 25 4
gpt4 key购买 nike

我正在尝试转换具有多个部分和动态行的 UITableView。

我目前正在尝试遍历 UITableView 的所有 subview 。我正在尝试将每个 subview 插入到一个容器 UIView 中,在那里我可以将它们全部组合起来构成一个 UIView,代表一个特定高度限制的页面,我可以在其中动态添加页脚。

基本上,目标是遍历所有部分,并将每个单元格的所有 View 合并到一个代表该部分(包括部分标题)的大 UIView 中。

这样我以后就可以轻松地将每个 UIView 转换为特定的 PDF 页面。就好像我直接将 UITableView 转换为 PDF 一样,我会在不需要的地方进行页面剪切。

最佳答案

我能够遍历所有 UITableView 部分并通过下面的代码行:

这里的逻辑是,为了能够获得我附加到 cell.contentView 的正确 subview ,我在将它们添加为 subview 之前为它们设置了一个标签,这样我就能够获得我添加的确切 View ,而不是包装 contentView。

var subviews = [UIView]()
let sectionCount = tableView.numberOfSections

/// loop through available sections
for section in 0..<sectionCount {

/// get each section headers
if let sectionHeader = self.tableView(tableView, viewForHeaderInSection: section) {
subviews.append(sectionHeader)
}


if tableView.numberOfRows(inSection: section) > 0 {
/// loop through rows within section
for row in 0..<tableView.numberOfRows(inSection: section) {
let indexPath = IndexPath(row: row, section: section)
tableView.scrollToRow(at: indexPath, at: .top, animated: false)
if let currentCell = tableView.cellForRow(at: indexPath), let view = currentCell.contentView.viewWithTag(999) {
subviews.append(view)
}
}
}
}

关于swift - 如何遍历所有 UITableView 部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48787605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com