ios - 如何在 swift 4 和 xcode 9 中单击 UITableViewCell 中的按钮更新 UILabel?

标签 ios swift xcode uitableview

<分区>

我正在构建一个订餐应用程序。其中,有递增和递减按钮和一个显示数量的 UILabel。我想在点击递增和递减按钮时更新数量标签。附上其图像。

enter image description here

My ViewController 的一个片段是

protocol GondolaTableViewCellDelegate: class {
    func tableViewCellAddToCart(_ sender: ItemDetailTableViewCell)
    func tableViewCellIncrement(_ sender: ItemDetailTableViewCell)
    func tableViewCellDecrement(_ sender: ItemDetailTableViewCell)
    var tableViewCellQuantity: String { get set }
}

class ItemDetailTableViewCell: UITableViewCell {
    //itemTableCell

    var quantity = 1

    @IBOutlet weak var itemNameLabelCell: UILabel!
    @IBOutlet weak var itemDescLabelCell: UILabel!
    @IBOutlet weak var itemPriceLabelCell: UILabel!
    @IBOutlet weak var itemQuantityLabelCell: UILabel!
    @IBOutlet weak var itemDecrementButton: UIButton!
    @IBOutlet weak var itemIncrementButton: UIButton!
    @IBOutlet weak var addToCartButton: UIButton!

    weak var delegate: GondolaTableViewCellDelegate?

    @IBAction func addToCartCellButton(_ sender: Any) {
        delegate?.tableViewCellAddToCart(self)

        //print("Neck, Angel Memory")
    }
    @IBAction func itemIncrementButtonCell(_ sender: Any) {
        delegate?.tableViewCellIncrement(self)

        //quantity  = quantity+1
        //itemQuantityLabelCell.text = "\(quantity)"
    }
    @IBAction func itemDecrementButtonCell(_ sender: Any) {
        delegate?.tableViewCellDecrement(self)

//        if quantity == 1 {
//            //toastNeck(message: "Min. quantity should be 1")
//        }else if quantity >= 2 {
//            //quantity  = quantity-1
//        }
        //itemQuantityLabelCell.text = "\(quantity)"
    }

}

class AllItemViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, GondolaTableViewCellDelegate {
    var tableViewCellQuantity: String = ""



    @IBOutlet weak var allItemImageHeader: UIImageView!
    @IBOutlet weak var allItemTableView: UITableView!
    @IBOutlet weak var allItemLabel: UILabel!
    @IBOutlet weak var visualEffect: UIVisualEffectView!
    @IBOutlet weak var itemsTableView: UITableView!
    @IBOutlet weak var itemDetailLabel: UILabel!
    @IBOutlet weak var cartItemLabel: UILabel!

    var storeItem = [StoreItem]()
    var allItem = [ItemDetaill]()
    var quantityArray: [Int] = []

    var selectedIndex: Int!

    var storeId: String = ""
    var storeCatId: String = ""
    var storeName: String = ""
    var quantity = 1

    override func viewDidLoad() {
        super.viewDidLoad()

        for i in 0 ..< 100 {
            quantityArray.append(2)
        }

        allItemTableView.delegate = self
        allItemTableView.dataSource = self
        itemsTableView.delegate = self
        itemsTableView.dataSource = self


    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return tableView === allItemTableView ? storeItem.count : allItem.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 1 //storeItem.count
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 5
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView()
        header.backgroundColor = UIColor.white
        return header
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return tableView === allItemTableView ? 56 : 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if tableView == allItemTableView {
            let storeCell = allItemTableView.dequeueReusableCell(withIdentifier: "allItemCell", for: indexPath) as! AllItemTableViewCell

            return storeCell
        }else {
            let itemCell = itemsTableView.dequeueReusableCell(withIdentifier: "itemTableCell", for: indexPath) as! ItemDetailTableViewCell
            itemCell.itemNameLabelCell.text = allItem[indexPath.section].item_name
            itemCell.itemDescLabelCell.text = allItem[indexPath.section].item_desc
            itemCell.itemPriceLabelCell.text = "₹" + allItem[indexPath.section].item_net_price
            itemCell.delegate = self
            itemCell.addToCartButton.tag = indexPath.section
            itemCell.addToCartButton.addTarget(self, action: #selector(addToCarts(_:)), for: .touchUpInside)
            itemCell.itemIncrementButton.tag = indexPath.section
            itemCell.itemIncrementButton.addTarget(self, action: #selector(increment(_:)), for: .touchUpInside)
            itemCell.itemDecrementButton.tag = indexPath.section
            itemCell.itemDecrementButton.addTarget(self, action: #selector(decrement(_:)), for: .touchUpInside)

            return itemCell
        }

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedIndex = indexPath.section
        if tableView == allItemTableView {

        }else {
            let itemCell = itemsTableView.cellForRow(at: indexPath)

            print("Will Work")
        }

    }

    @objc func increment(_ sender: UIButton) {
        quantity = quantity + 1
        tableViewCellQuantity = "\(quantity)"
        //let newQuantity = quantityArray[sender.tag] + 1
        //self.quantityArray.replaceSubrange(sender.tag, with: newQuantity)
        itemsTableView.reloadData()
    }

    @objc func decrement(_ sender: UIButton) {
        if quantity == 1 {
            toastNeck(message: "Min. quantity should be 1")
        }else if quantity >= 2 {
            quantity  = quantity - 1
        }
        tableViewCellQuantity = "\(quantity)"
        itemsTableView.reloadData()
    }

    @objc func addToCarts(_ sender: UIButton) {
        if sender.tag == 0 {
            print(storeItem[sender.tag].item_name)
        }
    }


    func tableViewCellAddToCart(_ sender: ItemDetailTableViewCell) {
        guard let tappedIndexPath = itemsTableView.indexPath(for: sender) else {
            return
        }

        print(allItem[tappedIndexPath.section].item_name)
    }

    func tableViewCellIncrement(_ sender: ItemDetailTableViewCell) {
        guard let tappedIndexPath = itemsTableView.indexPath(for: sender) else {
            return
        }

        print(allItem[tappedIndexPath.section].created_date)
        quantity = quantity + 1
        tableViewCellQuantity = "\(quantity)"
        //let newQuantity = quantityArray[tappedIndexPath.section] + 1
        //self.quantityArray.replaceSubrange(tappedIndexPath.count, with: <#T##Collection#>)

    }

    func tableViewCellDecrement(_ sender: ItemDetailTableViewCell) {
        guard let tappedIndexPath = itemsTableView.indexPath(for: sender) else {
            return
        }
        if quantity == 1 {
            toastNeck(message: "Min. quantity should be 1")
        }else if quantity >= 2 {
            quantity  = quantity - 1
        }
        tableViewCellQuantity = "\(quantity)"
        itemsTableView.reloadData()

        print(allItem[tappedIndexPath.section].id)
    }

    func tableViewCellQuantity(_ sender: ItemDetailTableViewCell) {

    }
}

但是我能够通过协议(protocol)检测按钮点击,但无法更新 UILabel。

我还需要将添加的项目存储到数据模型类中,并且必须是不同项目的不同值,这意味着每个项目应该存储不同的数量。

有个类似的问题here但它不起作用。

如果有人需要更多详细信息,请告诉我。

最佳答案

试试这个:

@objc func increment(_ sender: UIButton) {
       if let cell = sender.superview?.superview as? YourCellClass {
           let indexPath = tbl_songsInfo.indexPath(for: cell)
           //Do whatever you want

        quantity = quantity + 1
        tableViewCellQuantity = "\(quantity)"
        //let newQuantity = quantityArray[sender.tag] + 1
        //self.quantityArray.replaceSubrange(sender.tag, with: newQuantity)
        itemsTableView.reloadData()

        }

    }

看看这个link

关于ios - 如何在 swift 4 和 xcode 9 中单击 UITableViewCell 中的按钮更新 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50869215/

相关文章:

ios - 是否可以从 didTapOverlay 方法更改 GMSPolygon fillColor?

iphone - 当属性链接到 IBOutlet 时,到底调用什么初始值设定项?

ios - CALayer 委托(delegate)仅在使用 Swift 时偶尔调用

ios - 使用 Xcode 5.1.1 构建您的应用程序

iOS App,获取文本内容

ios - 使用 SwiftUI 时预览不将设备更改为 iPhone SE

ios - 在 UItextfield 中使用气泡自动完成

iphone - 迭代 NSString 数组并比较它们的更有效方法

swift 。如何删除 AVPlayerViewController 中的工具栏

java - String.data 到 java (Swift)