简体   繁体   中英

In tableview cell, button action not working

Here is the code I've already done:

  func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (deleteAction, indexPath) in 
    self.deleteApm(currentIndex: Int) // here getting error
    } 
    return [deleteAction] 
  }   
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
      cell.reSheduleBtn.addTarget(self, action: #selector(self.myReSheButt(_:)), for: .touchUpInside)

    return cell
}

@IBAction func myReSheButt(_ sender: UIButton) {
    reSheduleApm()
}

func reSheduleApm(){
    let jsonDict = self.ArrayList[indexPath.row] as? [String:Any]
    let appId:Int  = jsonDict?["appId"] as! Int
    let appId2 : Int = jsonDict?["appId"] as! Int
    var appString = String(appId2)
    let schedDay = jsonDict?["sDate"] as! String
    let params: [String : String] = ["day":schedDay,"appId":appString]

    let url = APIdata.URL_APP_RESHED

    Alamofire.upload(
        multipartFormData: { multipartFormData in

            for (key, value) in params
            {
                multipartFormData.append((value.data(using: .utf8))!, withName: key)
            }
    },
        to: url,
        encodingCompletion: { encodingResult in switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                print(response.value as Any)
                //                    self.ArrayList.remove(at: indexPath.row)
                //                    self.tblView.deleteRows(at: [indexPath], with: .fade)
            }
        case .failure(_):
            break
            }
    })
}

In tableview cell, button action not working even if the function writes outside.
Inside editActionsForRowAt in UITableview the function cannot call inside the myReSheButt button Action.

And the function cannot call inside the editActionsForRowAt Also getting error

How to solve this?

pass your tag in cell for row

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
 cell.reSheduleBtn.tag = indexPath.row
cell.reSheduleBtn.addTarget(self, action: #selector(self.myReSheButt(_:)), for: .touchUpInside)

return cell
}

and handle the action as

@objc func myReSheButt(_ sender: UIButton) {
reSheduleApm(currentIndex: sender.tag)
}

ans pass your currentIndex to your reSheduleApm

func reSheduleApm(currentIndex: Int){
    let jsonDict = self.ArrayList[currentIndex] as? [String:Any]
    let appId:Int  = jsonDict?["appId"] as! Int
    let appId2 : Int = jsonDict?["appId"] as! Int
    var appString = String(appId2)
    let schedDay = jsonDict?["sDate"] as! String
    let params: [String : String] = ["day":schedDay,"appId":appString]

    let url = APIdata.URL_APP_RESHED

    Alamofire.upload(
        multipartFormData: { multipartFormData in

            for (key, value) in params
            {
                multipartFormData.append((value.data(using: .utf8))!, withName: key)
            }
    },
        to: url,
        encodingCompletion: { encodingResult in switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                print(response.value as Any)
                //                    self.ArrayList.remove(at: indexPath.row)
                //                    self.tblView.deleteRows(at: [indexPath], with: .fade)
                //
            }
        case .failure(_):
            break
            }
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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