在表格单元格中,按钮操作不起作用

时间:2019-04-12 06:33:32

标签: ios swift uitableview uibutton

这是我已经完成的代码:

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

在表格单元格中,即使函数在外部编写,按钮操作也不起作用。
editActionsForRowAt的{​​{1}}内部,函数无法在UITableview按钮的Action内部调用。

该函数无法在editActionsForRowAt内部调用也会出错

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

在单元格中为行传递标签

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

将您的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
            }
    })
}

答案 1 :(得分:-1)

将Buton添加到tableview单元格中,示例如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cell=super.tableView(tableView, cellForRowAtIndexPath: indexPath)

            if indexPath.section == 1 && indexPath.row == 0{

            let button = UIButton(frame: CGRectMake(0, 50, 120, 30))

            button.setTitle("1", forState: UIControlState.Normal)

            button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)

            button.addTarget(self, action: "onClick", forControlEvents: UIControlEvents.TouchUpInside)

            cell .contentView.addSubview(button)

           }
           return cell
    }

onClick函数:

func onClick(){

        UIAlertView(title: "Title", message: "Normal", delegate: nil, cancelButtonTitle: "Sure", otherButtonTitles: "Cancle").show()
    }