Swift UITableViewCell 移除所有子视图:使用 map 函数
在 Swift 中,你可以使用遍历数组的 'map' 函数来移除 'UITableViewCell' 的所有子视图。首先,获取 'UITableViewCell' 的 'subviews' 数组,然后使用 'map' 函数遍历数组,移除每个子视图。
这是一个示例代码:
// 在 UITableViewDelegate 的方法中获取 UITableViewCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: 'Cell', for: indexPath)
// 移除所有子视图
cell.subviews.map { $0.removeFromSuperview() }
return cell
}
在这个示例中,我们假设你已经为 'UITableViewCell' 设置了 'ReuseIdentifier' 为 'Cell'。然后,我们在获取 'UITableViewCell' 时使用 'map' 函数来移除所有子视图。
请注意,这种方法会移除所有子视图,包括默认的 'contentView',所以你可能需要再次添加所需的子视图。
原文地址: https://www.cveoy.top/t/topic/qoLR 著作权归作者所有。请勿转载和采集!