0

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



}
6
  • post cellForRow(::) method Commented Oct 29, 2018 at 4:08
  • @SPatel can you please ilustrate a little more, I edited my question with more info to be more clear. cheers. Commented Oct 29, 2018 at 4:09
  • @SPatel tried this but no success. let cell = tableView.cellForRow(at: indexPath) as! ProductCell Commented Oct 29, 2018 at 4:17
  • You never assigned or appended any value to stackArray so it will remain empty. Commented Oct 29, 2018 at 4:23
  • @Kamran if I do cell.useGuideArray = ["auto","moto","truck","industrial"] it still sends empty values. Commented Oct 29, 2018 at 4:27

1 Answer 1

1

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)
         }
     }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I've updated the code once again. and added your solution. but still no progress.
You pasted this code at the wrong place. Remove it from the function setupLayout. Put this at the class level declaration of useGuideArray.
why simply a userGuideArray = [String]() wont do the work?? why the need of didset {} for that.. thanks! @Kamran
You are declaring useGuideArray with empty data. So when you were not seeing any view because the array was empty. When you are assigning the array with actual data then you need to add those views again because they were not added for the first time as the array was empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.