首页 > 解决方案 > 从 cellForRowAt 将字符串数组发送到自定义 UItableviewCell

问题描述

cellForRowAt我发送以下值

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

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ProductCell
    let product = productList[indexPath.row]

    cell.productImage.image = UIImage(named: product.productImage)
    cell.productName.text = product.productName
    cell.productDescription.text = product.productDescription
    cell.useGuideArray = product.productGuide // ["auto","moto","truck","industrial"]
    cell.accessoryType = .disclosureIndicator

    return cell
}

当我在自定义单元格上使用 print() 读取值时,它总是返回 []

我在自定义表格视图单元格中获取数据的方式是以下代码。

不工作

var useGuideArray = [String]()

stackview.addArrangedSubview(stackTitleLabel)

    for row in useGuideArray {
        let viewImage = UIImageView()
        viewImage.image = UIImage(named: row)
        stackview.addArrangedSubview(viewImage)
    }

有用吗

var useGuideArray = [String]()

stackview.addArrangedSubview(stackTitleLabel)

    for row in ["auto","moto","truck","industrial"] {
        let viewImage = UIImageView()
        viewImage.image = UIImage(named: row)
        stackview.addArrangedSubview(viewImage)
    }

更新代码:

class ProductCell: UITableViewCell {



    let productImage: UIImageView = {
        let label = UIImageView()
        label.contentMode = .scaleAspectFit
//        label.backgroundColor = UIColor.yellow
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    let productName: UILabel = {
        let label = UILabel()

        label.font = UIFont.systemFont(ofSize: 20).bold()
        label.textColor = UIColor.black
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .left
//        label.backgroundColor = UIColor.blue
        return label
    }()

    let productDescription: UILabel = {
        let label = UILabel()

        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.lightGray
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .left
//        label.backgroundColor = UIColor.red
        return label
    }()

    let useGuideStack: UIStackView = {
    let stack = UIStackView()
        stack.translatesAutoresizingMaskIntoConstraints = false
//        stack.backgroundColor = UIColor.green
        stack.alignment = .fill
        stack.distribution = .fillProportionally
        return stack
    }()


    let stackTitleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14).bold()
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .left
        label.text = "Guida de uso"
        return label
    }()


var useGuideArray = [String]()


    func setupLayout(){
//        useGuideArray.append("auto")
//        useGuideArray.append("industrial")
//        useGuideArray.append("truck")
//        print(useGuideArray)

        addSubview(productImage)
        addSubview(productName)
        addSubview(productDescription)
        addSubview(useGuideStack)

//        useGuideStack.addArrangedSubview(stackTitleLabel)
//
//        for row in useGuideArray {
//            let viewImage = UIImageView()
//            viewImage.image = UIImage(named: row)
//            viewImage.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
//            viewImage.contentMode = .scaleAspectFit
//            useGuideStack.addArrangedSubview(viewImage)
//        }


        var useGuideArray = [String]() {
            didSet {

                // Shows the content of `useGuideArray`
                print(useGuideArray)

                // Cleaning the stackView
                useGuideStack.subviews.forEach({ $0.removeFromSuperview() })

                // Adding the views
                useGuideStack.addArrangedSubview(stackTitleLabel)

                for row in useGuideArray {
                    let viewImage = UIImageView()
                    viewImage.image = UIImage(named: row)
                    useGuideStack.addArrangedSubview(viewImage)
                }
            }
        }



        let productImageConstrains = [
            productImage.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10.0),
            productImage.topAnchor.constraint(equalTo: self.topAnchor, constant: 10.0),
            productImage.heightAnchor.constraint(equalToConstant: 158),
            productImage.widthAnchor.constraint(equalToConstant: 85),
        ]

        NSLayoutConstraint.activate(productImageConstrains)

        let productNameConstrains = [
            productName.leftAnchor.constraint(equalTo: productImage.rightAnchor, constant: 10.0),
            productName.topAnchor.constraint(equalTo: self.topAnchor, constant: 10.0),
            productName.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -25.0),
            productName.heightAnchor.constraint(equalToConstant: 25),
        ]

        NSLayoutConstraint.activate(productNameConstrains)

        let productDescriptionConstrains = [
            productDescription.topAnchor.constraint(equalTo: productName.bottomAnchor, constant: 5.0),
            productDescription.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -24.0),
            productDescription.leftAnchor.constraint(equalTo: productImage.rightAnchor, constant: 10.0),
            productDescription.bottomAnchor.constraint(equalTo: useGuideStack.topAnchor)
        ]

        NSLayoutConstraint.activate(productDescriptionConstrains)


        let useGuideStackConstrains = [
//            useGuideStack.topAnchor.constraint(equalTo: productDescription.bottomAnchor, constant: 5.0),
            useGuideStack.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -24.0),
            useGuideStack.leftAnchor.constraint(equalTo: productImage.rightAnchor, constant: 10.0),
            useGuideStack.heightAnchor.constraint(equalToConstant: 45),
            useGuideStack.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10.0)
        ]

        NSLayoutConstraint.activate(useGuideStackConstrains)

    }

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

        // Configure the view for the selected state
    }


    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupLayout()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



}

标签: swiftuitableview

解决方案


我认为您正在尝试在stackViewuseGuideArray分配实际data. 而从 中cellForRowAt,很明显你并没有subViews在分配后再次调用与添加相关的代码data。解决您的问题可能是使用didSet回调添加subViews如下,

var useGuideArray = [String]() {
    didSet {

         // Shows the content of `useGuideArray`
         print(useGuideArray)

         // Cleaning the stackView
         stackview.subviews.forEach({ $0.removeFromSuperview() })

         // Adding the views
         stackview.addArrangedSubview(stackTitleLabel)

         for row in useGuideArray {
            let viewImage = UIImageView()
            viewImage.image = UIImage(named: row)
            stackview.addArrangedSubview(viewImage)
         }
     }
}

推荐阅读