动态添加子视图到 UITableViewCell [英] Dynamically add subViews to UITableViewCell

查看:24
本文介绍了动态添加子视图到 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 UILabels 添加到 UITableViewCell,但它的动态,第一个单元格可以有 1 个标签,第二个可以有 4 个,我事先不知道.所以我尝试了这个

I need to add UILabels to UITableViewCell, but its dynamic, first cell can have 1 label, second can have 4 and I dont know before hand. So I tried this

 func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

        let cell: ReviewTableViewCell = reviewTableView.dequeueReusableCell(withIdentifier: "Review", for: indexPath) as! ReviewTableViewCell

        var reviewObj:Review!

        reviewObj = reviewArray[(indexPath as NSIndexPath).row]


        let viewsAdded = commentViewsAddedDict[indexPath.row]

        if(viewsAdded == nil)
        {
            for comment in reviewObj.commentArray
            {
                let label1 = UILabel()
                label1.text = "text1"
                label1.textColor =  UIColor(hexString: "#333333")

                let label2 = UILabel()
                label2.text =  "text2"
                label2.numberOfLines = 0
                label2.sizeToFit()
                label2.textColor =  UIColor(hexString: "#666666")

                let label3 = UILabel()
                label3.text = "----------------------------------------------------------------------"
                label3.textColor =  UIColor(hexString: "#eeeeee")


                cell.stackView1.addArrangedSubview(label1)
                cell.stackView1.addArrangedSubview(label2)
                cell.stackView1.addArrangedSubview(label3)
            }

             commentViewsAddedDict[indexPath.row] = true
        }

        return cell
    }

但是发生了什么,之前添加的视图没有被删除,它再次尝试添加新视图.

But what happens, the previously added views are not removed and it again tries to add new views.

所以我想知道,这样做的有效方法是什么.其次,我哪里出错了.

So I want to know, what is the efficient way to do this. Secondly, where I am going wrong.

问候兰吉特

推荐答案

您正在使用 commentViewsAddedDict 来确定是否已添加行.但是这些标签是否添加不是表格中行的函数,而是单元格的函数,它被重复使用.

You are using commentViewsAddedDict to figure out whether rows have been added or not. But whether these labels were added or not is not a function of the row in the table, but rather of the cell, which is reused.

所以,我会建议:

  • 消除这个 commentViewsAddedDict 逻辑;和

移动有关向 ReviewTableViewCell 添加多少标签的逻辑.

move the logic regarding how many labels have been added to ReviewTableViewCell.

所以,你可能会得到:

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
    let cell = reviewTableView.dequeueReusableCell(withIdentifier: "Review", for: indexPath) as! ReviewTableViewCell

    var reviewObject = reviewArray[(indexPath as NSIndexPath).row]

    cell.updateLabels(for: reviewObject)

    return cell
}

ReviewTableViewCell中:

func updateLabels(for reviewObject: ReviewObjectType) {
    // add label if needed

    // update label `text` if needed

    // remove any labels that need to be removed
}

要具体说明 updateLabels 中的逻辑有点困难,因为问题中提供的代码片段不清楚,但基本思想是 ReviewTableViewCell 应该保持跟踪其标签是否已添加,并根据 reviewObject 决定是否需要添加标签、更新现有标签或删除任何不需要的标签 reviewObject代码>审查对象.但是所有这些标签状态"逻辑都是单元格的函数(可以重复使用),而不是单元格对应的表格中的哪一行.

It's a little hard to be specific on the logic in updateLabels as the provided code snippet in the question is unclear, but the basic idea is that the ReviewTableViewCell should keep track of whether its labels have been added or not, and on the basis of the reviewObject, decide whether it needs to add a label, update an existing label, or remove any labels not needed for this particular reviewObject. But all of this "label state" logic is a function of the cell (which can be reused), not of which row in the table to which the cell corresponds.

这篇关于动态添加子视图到 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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