我有一个数组,我循环并将值传递给我的表格视图单元格标签。但是当我传递值时,标签只显示数组的最后一个值。我希望它显示来自数组的所有项目。我怎么能证明这一点?
let addonDescrip = ItemDataSource.sharedInstance.items[indexPath.row].addonDescp
print(addonDescrip)
for items in addonDescrip{
print(items)
cell.descriptionLbl.text = items
print(cell.descriptionLbl.text)
}
我希望它按以下顺序显示项目:
item1,
item2,
item4
我们如何在表格视图单元格标签中显示这样的内容? 我的表视图委托,
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ItemDataSource.sharedInstance.items.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 72
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = cartTableView.dequeueReusableCell(withIdentifier: "cartCell", for: indexPath) as! CartTableViewCell
cell.dishTitleLbl.text = ItemDataSource.sharedInstance.items[indexPath.row].itemName!
cell.priceLbl.text = ItemDataSource.sharedInstance.items[indexPath.row].totalPrice!
print(cell.priceLbl.text!)
let addonDescrip = ItemDataSource.sharedInstance.items[indexPath.row].addonDescp
print(addonDescrip)
for items in addonDescrip{
print(items)
cell.descriptionLbl.text = items
print(cell.descriptionLbl.text)
}
cell.quantityLbl.text = "1"//String(ItemDataSource.sharedInstance.items[indexPath.row].cartItem)
itemname = cell.dishTitleLbl.text!
itemDescription = cell.descriptionLbl.text!
itemprice = cell.priceLbl.text!
itemID = ItemDataSource.sharedInstance.items[indexPath.row].itemID!
cell.deleteBtn.tag = indexPath.row
cell.addBtn.tag = indexPath.row
cell.minusBtn.tag = indexPath.row
cell.deleteBtn.addTarget(self, action: #selector(crossBtnTapped), for: .touchUpInside)
cell.quantityLbl.layer.cornerRadius = 3
cell.quantityLbl.layer.borderColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
cell.quantityLbl.layer.borderWidth = 1.5
cell.selectionStyle = .none
cell.delegate = self
return cell
}
答案 0 :(得分:0)
如果要在一个标签中显示所有项目,请按照
添加字符串 for (index,item) in addonDescrip.enumerated() {
print(items)
if index == 0 {
cell.descriptionLbl.text = item
} else {
cell.descriptionLbl.text = cell.descriptionLbl.text + "," + item
}
print(cell.descriptionLbl.text)
}
或强>
cell.descriptionLbl.text = addonDescrip.joined(separator: ",")