简体   繁体   English

在UITableView内以编程方式将UIViews添加到UIStackView

[英]Add UIViews to UIStackView programmatically inside UITableView

I have three elements UILabel , UIImageView and UIButton in a list. 我在列表中有三个元素UILabelUIImageViewUIButton I have to show them accordingly inside UITableView . 我必须在UITableView相应地显示它们。 I have an array like this: 我有一个像这样的数组:

tableArray = [["label", "img", "button"],["img","button","label"],["img","label","button"],["button", "img", "label"]]

The positions of the elements are same as the positions of them (index) inside the array. 元素的位置与数组内它们的位置(索引)相同。 My cellForRowAt looks like this: 我的cellForRowAt看起来像这样:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "FirstTableViewCell", for: indexPath) as? FirstTableViewCell else {
        return UITableViewCell()
    }

    let tableData = tableArray[indexPath.row]

    var count = 0
    tableData.forEach { (element) in
        switch element {
        case "label":
            let lbl = self.createLabel()

            cell.stackView.insertArrangedSubview(lbl, at: count)
            count += 1
            break
        case "img":
            let img = self.createImage()
            cell.stackView.insertArrangedSubview(img, at: count)
            count += 1
            break
        case "button":
            let btn = self.createButton()
            cell.stackView.insertArrangedSubview(btn, at: count)
            count += 1
            break
        default:
            break
        }
    }
    return cell
}

The problem is whenever I am scrolling TableView every time those items are added into the cells. 问题是,每当这些项添加到单元格中时,每当我滚动TableView时。

I have tried few solutions to solve that but no luck. 我尝试过很少的解决方案来解决该问题,但是没有运气。

  1. if tableView.cellForRow(at: indexPath) == nil then add elements into stackviews. if tableView.cellForRow(at: indexPath) == nil则将元素添加到stackviews中。

  2. Checking if cell == nil after dequeueReusableCell . dequeueReusableCell之后检查if cell == nil If nil then init the cell. 如果为零,则init单元格。

  3. let cell = UITableViewCell.init(style: .default, reuseIdentifier: nil) as? FirstTableViewCell let cell = UITableViewCell.init(style: .default, reuseIdentifier: nil) as? FirstTableViewCell tried even without dequeueReusableCell . 即使没有dequeueReusableCell let cell = UITableViewCell.init(style: .default, reuseIdentifier: nil) as? FirstTableViewCell尝试过。

But the same thing happens every time. 但是每次都会发生同样的事情。

This is the FirstTableViewCell 这是FirstTableViewCell

class FirstTableViewCell: UITableViewCell {

    @IBOutlet weak var stackView: UIStackView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

Any idea how to check if elements are already added to StackView , then I'll not add them. 任何想法如何检查元素是否已经添加到StackView ,那么我就不会添加它们。

Because the cells are reused, the first thing you need to do in cellForRowAt is "reset" your cell... in other words, clear out the existing subviews from the stack view: 由于单元已被重用,因此您需要在cellForRowAt做的第一件事是“重置”您的单元...换句话说,从堆栈视图中清除现有的子视图:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "FirstTableViewCell", for: indexPath) as? FirstTableViewCell else {
        return UITableViewCell()
    }

    // remove the views that are currently in the stack
    cell.stackView.arrangedSubviews.forEach {
        $0.removeFromSuperview()
    }

    // the rest of your setup
    let tableData = tableArray[indexPath.row]

    var count = 0
    tableData.forEach { (element) in
    ...
}

Now, based on the code you presented, if the cells always contain a UILabel , UIImageView and UIButton , just in different orders and with different properties, you could add them once when you create the cell (or add them in your cell prototype if you're using one), and then simply re-arrange them as needed. 现在,根据您提供的代码,如果单元格始终包含UILabelUIImageViewUIButton ,只是顺序不同且具有不同的属性,则可以在创建单元格时将它们添加一次(或者将它们添加到单元格原型中)。 '),然后根据需要简单地重新排列它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM