【问题标题】:In tableview cell, button action not working在 tableview 单元格中,按钮操作不起作用
【发布时间】:2019-09-02 20:57:10
【问题描述】:

这是我已经完成的代码:

  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
            }
    })
}

在tableview单元格中,即使函数在外部写入,按钮操作也不起作用。
UITableview中的editActionsForRowAt内部,函数无法在myReSheButt按钮操作内部调用。

而且函数不能在editActionsForRowAt里面调用也报错

如何解决?

【问题讨论】:

  • 请正确格式化您的代码。目前,它不会编译,因为您有许多嵌套函数。您至少缺少一个右花括号。
  • 选择器可以调用以@objc 开头的函数,除非你有充分的理由使用@IBAction,否则你可能不应该

标签: ios swift uitableview uibutton


【解决方案1】:

在单元格中传递您的标签以获取行

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
}

并将动作处理为

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

ans 将您的 currentIndex 传递给您的 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
            }
    })
}

【讨论】:

  • 使用“currentIndex”选择的行按钮不调用另一行正在调用...
  • 是的,我没有添加这一行“cell.reSheduleBtn.tag = indexPath.row”所以我得到了问题,这个答案是正确的,谁反对这个?,原因是什么?
  • @SruthiCSankar - 很简单,在这一行使用self.deleteApm(currentIndex: indexPath.row) // 这里出错
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多