我能够通过下面的代码遍历所有 UITableView 部分和行:
这里的逻辑是,为了能够获得我附加到 cell.contentView 的正确子视图,我在将它们添加为子视图之前使用标签设置它们,以便我能够获得确切的视图我添加了而不是包装 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)
}
}
}
}