如何删除或查看表视图行

问题描述 投票:0回答:1

我在表viewenter image description here的每一行都有一个视图和删除按钮。我想点击每个删除或查看里面的项目。我添加了两个按钮功能,但是如何知道当我点击删除或查看第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)
}
ios swift
1个回答
0
投票

我在这里推荐代理模式,如下所示:

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
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.