从cellForRowAt将字符串数组发送到自定义UItableviewCell

时间:2018-10-29 04:05:34

标签: swift uitableview

来自 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")
    }



}

1 个答案:

答案 0 :(得分:1)

我认为当stackView未分配实际的useGuideArray时,您正在尝试在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)
         }
     }
}