Dynamically add subViews to UITableViewCell

只谈情不闲聊 提交于 2019-12-12 03:06:39

问题


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.

Regards Ranjit


回答1:


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.

So, I'd would advise:

  • eliminate this commentViewsAddedDict logic; and

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

So, you might end up with:

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
}

And in ReviewTableViewCell:

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

    // update label `text` if needed

    // remove any labels that need to be removed
}

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.



来源:https://stackoverflow.com/questions/39683959/dynamically-add-subviews-to-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!