我目前正在从事一个基于 Swift 的 HRM 项目。它需要显示带有稍微自定义单元格的表格 View 。它本身包含两个按钮的单元格,在某些业务逻辑下,一个按钮将被隐藏。例如,
如果当前用户是员工本人,他可以看到一个列表,包含他名字的单元格可以看到两个按钮,但其他单元格只会显示一个按钮。 我尝试了以下方法: 1. 如果 userId == employeeId(employeeId 来自模型)那么,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ClaimTableViewCell", for: indexPath) as! ClaimTableViewCell
if(self.claimdata[indexPath.section].employeeId == self.empId) {
cell.CancelButton.isHidden = false
}
我也试过了
if(self.claimdata[indexPath.section].employeeId != self.empId) {
cell.CancelButton.frame.size.height = 0
}
第一帧工作正常,当我开始滚动时问题就开始了。对于一些不需要的单元格,它还会显示两个按钮。
我错过了什么吗?
最佳答案
此问题是由于 UITableView 中的单元格可重用性引起的。
在您的 cellForRowAtIndexPath 方法中使用以下代码。
cell.CancelButton.isHidden = true
if(self.claimdata[indexPath.section].employeeId == self.empId) {
cell.CancelButton.isHidden = false
}
关于ios条件tableview单元格渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386452/