Eddwin Paz
Eddwin Paz

Reputation: 2868

Sending String Array to Custom UItableviewCell from cellForRowAt

from cellForRowAt I send the following value

  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
}

when I read the value using print() on the custom cell it always returns []

the way I'm grabbing the data in my custom tableview cell is the following code.

Does Not Work

var useGuideArray = [String]()

stackview.addArrangedSubview(stackTitleLabel)

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

Does Work

var useGuideArray = [String]()

stackview.addArrangedSubview(stackTitleLabel)

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

UPDATED CODE:

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



}

Upvotes: 0

Views: 52

Answers (1)

Kamran
Kamran

Reputation: 15238

I think you are trying to add views in the stackView when the useGuideArray is not assigned with the actual data. And from the cellForRowAt, it is clear that you are not calling the code related to adding subViews again after assigning the data. A fix you problem could be to use the didSet callback to add subViews as below,

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)
         }
     }
}

Upvotes: 1

Related Questions