繁体   English   中英

快速将数组项传递到表视图单元格

[英]Pass array items to table view cell in swift

我正在遍历一个数组,并将该值传递给表视图单元格标签。 但是当我传递值时,标签仅显示数组的最后一个值。 我希望它显示来自数组的整个项目。 我如何证明这一点?

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
}

如果要在一个标签中显示所有项目,请附加这样的字符串

    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: ",")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM