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