【问题标题】:ios conditional tableview cell renderingios条件tableview单元格渲染
【发布时间】: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


【解决方案1】:

这个问题是由于 UITableView 中的单元格可重用性造成的。

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

cell.CancelButton.isHidden = true

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

【讨论】:

    【解决方案2】:

    由于tableView单元格是reusableCell

    带标识符的dequeueReusableCell

    您只需要提供 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
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 2018-09-30
      • 2020-08-17
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 2021-09-07
      • 2013-10-11
      • 2011-10-02
      相关资源
      最近更新 更多