【发布时间】:2019-04-22 12:15:13
【问题描述】:
我目前正在从事一个基于 swift 的 HRM 项目。它需要显示带有稍微自定义单元格的表格视图。它本身包含两个按钮的单元格,在某些业务逻辑下,一个按钮将被隐藏。例如,
如果当前用户是员工本人,他可以看到一个列表,包含他姓名的单元格可以看到两个按钮,但其他单元格只会显示一个按钮。 我尝试了以下方法: 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
}
第一帧工作正常,当我开始滚动时问题就开始了。对于一些意外的单元格,它还会显示两个按钮。
我错过了什么吗?
【问题讨论】:
-
由于 tableView 单元格是 reusableCell dequeueReusableCell withIdentifier 你只需要给出 else 条件,所以当它再次重用单元格时它知道如何处理 CancelButton。
标签: ios swift uitableview view uibutton