ios条件tableview单元渲染

ios条件tableview单元渲染,ios,swift,uitableview,view,uibutton,Ios,Swift,Uitableview,View,Uibutton,我目前正在从事一个基于swift的人力资源管理项目。其中需要显示带有稍微定制的单元格的tableview。单元格本身包含两个按钮,在某些业务逻辑下,一个按钮将被隐藏。比如说, 如果当前用户是员工本人,他可以看到一个列表,包含他姓名的单元格可以看到两个按钮,但另一个单元格只显示一个按钮。 我尝试了以下方法: 1.如果userId==employeeId(employeeId来自模型),那么 我也试过了 if(self.claimdata[indexPath.section].employeeId

我目前正在从事一个基于swift的人力资源管理项目。其中需要显示带有稍微定制的单元格的tableview。单元格本身包含两个按钮,在某些业务逻辑下,一个按钮将被隐藏。比如说,

如果当前用户是员工本人,他可以看到一个列表,包含他姓名的单元格可以看到两个按钮,但另一个单元格只显示一个按钮。 我尝试了以下方法: 1.如果userId==employeeId(employeeId来自模型),那么

我也试过了

if(self.claimdata[indexPath.section].employeeId  != self.empId) {

                cell.CancelButton.frame.size.height = 0


            }
第一帧效果很好,问题从我开始滚动开始。对于一些非预期的单元格,它还显示两个按钮


我遗漏了什么吗?

因为tableView单元格是可重用的单元格

带标识符的可重用单元出列

您只需要给else条件,这样当它再次重用单元格时,它就知道如何使用CancelButton

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

        }else{

            cell.CancelButton.isHidden = true
        }
    }

因为tableView单元是可重用的单元

带标识符的可重用单元出列

您只需要给else条件,这样当它再次重用单元格时,它就知道如何使用CancelButton

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

        }else{

            cell.CancelButton.isHidden = true
        }
    }

此问题是由于UITableView中的单元可重用性造成的

在您的cellForRowAtIndexPath方法中使用以下代码

cell.CancelButton.isHidden = true

if(self.claimdata[indexPath.section].employeeId  == self.empId) {
        cell.CancelButton.isHidden = false
 }

此问题是由于UITableView中的单元可重用性造成的

在您的cellForRowAtIndexPath方法中使用以下代码

cell.CancelButton.isHidden = true

if(self.claimdata[indexPath.section].employeeId  == self.empId) {
        cell.CancelButton.isHidden = false
 }

由于tableView单元格是reusableCell dequeueReusableCell with Identifier,您只需给出else条件,因此当它再次重用单元格时,它知道如何使用CancelButton。由于tableView单元格是reusableCell dequeueReusableCell with Identifier,您只需给出else条件,因此当它再次重用单元格时,它知道如何处理取消按钮。