UITableViewCell 在每 5 个单元格之后重复一次(在您标记为重复之前,请阅读整个任务) [英] UITableViewCell is repeating after every 5th cell (Before you mark as duplicate kindly read the whole quest )

查看:24
本文介绍了UITableViewCell 在每 5 个单元格之后重复一次(在您标记为重复之前,请阅读整个任务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(在标记为重复之前,您必须阅读整个问题,我发布此问题是因为我没有找到相关且正确的解决方案,也需要快速解决方案)

(Before you mark as duplicate you have to read the whole question and I am posting this cause I din't found the relevant and proper solution also need the solution in swift)

我创建了一个演示项目,并从自定义单元上的数组加载和显示名称和区域.我注意到在每 5 个单元格后意味着第 6 行与第 0 个单元格的内容重复例如

I have created one demo project and load and displayed name and area from array on custom cell. I have noticed that after every 5th cell means 6th row is repeating with contents of 0th cell for e.g.

演示代码如下

class demoTableCell: UITableViewCell {
    @IBOutlet var name : UILabel!
    @IBOutlet var area : UILabel!
}

extension ViewController:UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.arrDemo.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! demoTableCell

            cell.name.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Name") as? String
            cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String

            if indexPath.row == 0{
                cell.name.isHidden = true
            }

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
}

当我隐藏第 0 个单元格上的第一个标签时,我发现第 6 行也受到第 0 个单元格的已实现功能的影响.这意味着还要隐藏每 6 个单元格的 label1,因为我附上了下面的屏幕截图,以便您可以得到确切的问题(此问题仅在表格视图可滚动时发生)

As I hide the first label on 0th cell so I found that 6th row is also effected with implemented functionality of 0th cell. It means that also hide label1 of every 6th cell as I have attached the screenshot below so you can get the exact issue (This issue happened only if table view is scrollable)

因为我试图从 解决这个问题此链接也面临同样的问题,无法找到合适的解决方案,我被困在这里.

As I have try to solve this issue from this link also but facing the same issue and cannot find the proper solution, and I am stuck here.

推荐答案

记住 - 单元格正在被重用.

Remember - the cells are being reused.

您隐藏了单元格,但从未明确取消隐藏单元格

You hide the cell, but you never explicitly unhide the cell

当您来到第 6 行时,您正在重新使用位于第 0 行的单元格,并且 isHidden = true

When you come to row 6, you are re-using the cell that was at row 0, and isHidden = true

您需要做的就是扩展您的检查,隐藏您需要隐藏的行,并明确显示您需要查看的单元格.如果您还添加了移动横幅 - 您还需要检查它是否已加载,如果不需要,请将其删除.请记住 - 它可能不是第 6 行 - 这就是当前屏幕大小的处理方式

All you need to do is extend your check, and hide the rows that you need to be hidden, and explicitly show the cells that you need to see. If you also have a moving banner that you add - you will also need to check to see if it's been loaded, and remove it if not required. Remember - it may not be row 6 - that's just how it works out with the current screensize

如果您要使用的单元格之间确实存在显着差异,则最好使用两个不同的类 - 这样您就不必考虑隐藏标签

If you do have significant differences between the cells you want to use, you might be better using two different classes - and then you don't have to think about hiding labels

class demoTableCell: DemoTableCellNormalRow {
    @IBOutlet var name : UILabel!
    @IBOutlet var area : UILabel!
}

class demoTableCell: DemoTableCellFirstRow {
    @IBOutlet var area : UILabel!
    @IBOutlet var movingBannerView : LCBannerView!
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"

    if row == 0 {
        var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellFirstRow

        cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String

        // populate the bannerview which already exists, or create a new one

        return cell
    } else {
        var cell : demoTableCell = demoTable.dequeueReusableCell(withIdentifier: cellIdentifier)! as! DemoTableCellNormalRow

        cell.name.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Name") as? String
        cell.area.text = (arrDemo.object(at: indexPath.row) as! NSDictionary).value(forKey: "Area") as? String

        return cell
    }
}

这篇关于UITableViewCell 在每 5 个单元格之后重复一次(在您标记为重复之前,请阅读整个任务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆