【问题标题】:By clicking a cancel button in the cell to delete that cell , and also delete the cell by editActionsForRowAt method通过单击单元格中的取消按钮来删除该单元格,并通过 editActionsForRowAt 方法删除该单元格
【发布时间】:2019-04-08 08:23:32
【问题描述】:

使用UITableView 编辑方法可以正常工作,但使用UIButton 则无法正常工作。

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (deleteAction, indexPath) in
        deleteApm(Id: myString)
        self.AArrayList.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
    return [deleteAction]
}

func deleteApm(Id:String)
{
    //...................................
}

@IBAction func myButt(_ sender: UIButton) {
    let cancelBtn = sender.tag
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "ACell", for: indexPath) as! ATableViewCell
    cell.cancelBtn.addTarget(self, action: #selector(self.myButt(_:)), for: .touchUpInside)
    return cell
}

使用这个我在按钮中调用了相同的删除方法,但使用UIButton 删除不起作用。 使用UITableView 工作正常...

【问题讨论】:

    标签: ios swift uitableview button


    【解决方案1】:

    在 cellforRow 中为您的按钮添加 标签,以识别行或特定对象

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ACell", for: indexPath) as! ATableViewCell
         cell.cancelBtn.tag = indexPath.row
         cell.cancelBtn.addTarget(self, action: #selector(self.myButt(_:)), for: .touchUpInside)
     }
    

    并将动作处理为

    @IBAction func myButt(_ sender: UIButton) {
        let indexPath: IndexPath = IndexPath(row: sender.tag, section: 0)
        deleteApm( currentIndexpath: indexPath)
    
    }
    

    以及在 deleteRow 中调用

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (deleteAction, indexPath) in
            self.deleteApm(currentIndexpath: indexPath)           
        }
        return [deleteAction]
    }
    

    终于创建了delete api调用的常用方法

    func deleteApm(currentIndexpath: IndexPath )    {
        let jsonDict = self.ArrayList[currentIndexpath.row] as? [String:Any]
        let Id:Int = jsonDict?["appointId"] as! Int
        let Id2 : Int = jsonDict?["appointId"] as! Int
        let param : [String : String] = ["pid": {String(Id2)}()]
        let headers = ["Content-Type": "application/x-www-form-urlencoded"]
        // "http://192.168.1.45:8080/zybrodental/patientApp/patientAppDeleteMultipleAppointment"
    
        Alamofire.request(APIMetadata.URL_DELETE_APPOINT, method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: headers).responseJSON { (response:DataResponse<Any>) in
    
            switch(response.result) {
            case.success(let data):
                print("success",data)
                 self.ArrayList.remove(at: currentIndexpath.row)
                 self.yourtableViewName.deleteRows(at: [currentIndexpath], with: .fade)
    
            case.failure(let error):
                print("Not Success",error)
            }
        }
    }
    

    【讨论】:

    • 在最后一行代码中出现错误...在 tableView.deleteRows(at: [indexPath], with: .fade) 中对成员 'tableView(_:numberOfRowsInSection:) 的不明确引用
    • @SruthiCSankar - 请检查更新的答案,你能显示你的错误
    • 但我也想从“func deleteApm”中删除它
    • @SruthiCSankar- 你能显示 func deleteApm 的代码吗
    • @IBAction func myButt(_ sender: UIButton) { // let cancelBtn = sender.tag self.AArrayList.remove(at: sender.tag) let indexPath = NSIndexPath(row: sender.tag, section: 0) tblView.deleteRows(at: [indexPath as IndexPath], with: .fade) }
    【解决方案2】:

    首先,您在myButt(_ sender: UIButton) 函数中使用Sender.tag 然后,您必须从cellForRowAt tableView 方法中分配该标签。 请使用这个cellForRowAt 方法。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let cell = tableView.dequeueReusableCell(withIdentifier: "ACell", for: indexPath) as? ATableViewCell
         cell?.lblText.text = "\(arrIndex[indexPath.row])"
         cell?.btnDelete.tag = indexPath.row
         cell?.btnDelete.addTarget(self, action: #selector(self.myButt(_:)), for: .touchUpInside)
    
         return cell ?? UITableViewCell()
    }
    

    如果你想在删除按钮动作事件中删除特定的单元格,使用这个

    @IBAction func myButt(_ sender: UIButton) {
        let cancelBtn = sender.tag
        let indexPath = IndexPath(row: arrIndex.index(where: {$0 == "\(cancelBtn)"}) ?? 0, section: 0)
        self.arrIndex.remove(at: sender.tag)
        tableView.deleteRows(at: [indexPath], with: .fade)
        tableView.reloadData()
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多