簡體   English   中英

如何在表格視圖行中刪除或查看

[英]How to delete or view at table view row

我在表視圖的每一行中都有一個視圖和刪除按鈕,請在此處輸入圖像描述 我想單擊每個以刪除或查看其中的項目。 我添加了兩個按鈕功能,但是如何知道單擊刪除或查看詳細信息第2行時它將刪除或查看第2行?

@IBAction func deleteBtn(_ sender: Any) {
    let refreshAlert = UIAlertController(title: "Message", message: "Are you sure you want to remove this item from the cart?", preferredStyle: UIAlertControllerStyle.alert)

    refreshAlert.addAction(UIAlertAction(title: "No", style: .cancel, handler: { (action: UIAlertAction!) in

    }))

    refreshAlert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action: UIAlertAction!) in

        self.removeCartAPI()

        let buttonTag = (sender as AnyObject).tag


        //self.navigationController?.popViewController(animated: true)
    }))

    present(refreshAlert, animated: true, completion: nil)
}

@IBAction func viewDetailsBtn(_ sender: Any) {
    let vc: ParcelSendParcelSummaryViewController? = self.storyboard?.instantiateViewController(withIdentifier: "pSummaryVC") as?ParcelSendParcelSummaryViewController
    self.navigationController?.pushViewController(vc!, animated: true)
}

我在這里推薦委托模式,如下所示:

protocol YourCellNameDelegate {
    func didTapDelete(at cell: YourCellName)
    func didTapView(at cell: YourCellName)
}

class YourCellName: UITableViewCell {
    weak var delegate: YouCellNameDelegate?
    ...
    @IBAction func didTouchDeleteButton(sender: Any) {
        delegate?.didTapDelete(at: self)
    }
    // same for did tap view
}

class ParcelSendParcelSummaryViewController {
    ....
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(
            withIdentifier: "Your identifier",
            for: indexPath
        ) as? YourCellName {
            cell.delegate = self
        }
    }
}

extension ParcelSendParcelSummaryViewController: YouCellNameDelegate {
    func didTapDelete(at cell: YourCellName){
        if let index = tableView.indexPathForCellView(cell: cell) {
            tableView.beginUpdates()
            tableView.deleteRows(at: index, with: .automatic)
            // Make sure you delete the item from the data source here, between begin and end updates
            tableView.endUpdates()
        }
    }

    func didTapView(at cell: YourCellName) {
    // same as above, with your view logic
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM