当试图在我的 UITableViewCells 周围设置边框时,我遇到了一个问题,即每当向下滚动 TableView 时,都会在我不希望它出现的地方添加一个边框!我需要为第一个和最后一个单元格单独设置这些边框,因为 TableView 本身有一个我不想包含在边框中的标题。我已经有一个页脚,所以我也不能使用该选项。
这是我的代码的样子:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.viewMain.addBorders(edges: [.left, .right], color: UIColor.black)
if indexPath.row == 0 {
cell.viewMain.addBorders(edges: [.top], color: UIColor.black)
} else if indexPath.row == ((self.dataArray.count ?? 1) - 1){
cell.viewMain.addBorders(edges: [.bottom], color: UIColor.black)
}
这是我向下滚动和向后滚动时 TableView 的样子:为了保密,我不得不编辑数据,但看到问题了吗?边框应该只在顶部和底部,当 View 最初加载时,它看起来是正确的。只有当用户向下滚动或屏幕不够大而无法看到整个 TableView 时,我才会遇到此问题。
最佳答案
因为单元格被回收了,所以在添加边框之前需要去掉所有的边框
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
// remove all borders here
cell.viewMain.addBorders(edges: [.left, .right], color: UIColor.black)
if indexPath.row == 0 {
cell.viewMain.addBorders(edges: [.top], color: UIColor.black)
} else if indexPath.row == ((self.dataArray.count ?? 1) - 1){
cell.viewMain.addBorders(edges: [.bottom], color: UIColor.black)
}
关于ios - 向正确的 UITableViewCell 添加边框时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62882513/