为了账号安全,请及时绑定邮箱和手机立即绑定

表格视图单元格中的UIButton操作

表格视图单元格中的UIButton操作

iOS
呼唤远方 2019-10-17 16:14:50
我正在尝试为在表格视图单元格中按下的按钮运行操作。下面的代码在我的表视图控制器类中。在我的UITableViewCell类的称为requestCell的插座中,该按钮已被描述为“是”。我正在使用“解析”来保存数据,并且想在按下按钮时更新对象。我的objectIds数组可以正常工作,cell.yes.tag还会在日志中打印正确的数字,但是,为了正常运行查询,我无法将该数字输入“连接”函数中。我需要一种获取单元格的indexPath.row的方法,以找到正确的objectId。    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as requestsCell    // Configure the cell...    cell.name.text = requested[indexPath.row]    imageFiles[indexPath.row].getDataInBackgroundWithBlock{        (imageData: NSData!, error: NSError!) -> Void in        if error == nil {            let image = UIImage(data: imageData)            cell.userImage.image = image        }else{            println("not working")        }        }    cell.yes.tag = indexPath.row    cell.yes.targetForAction("connected", withSender: self)    println(cell.yes.tag)    return cell}func connected(sender: UIButton!) {    var query = PFQuery(className:"Contacts")    query.getObjectInBackgroundWithId(objectIDs[sender.tag]) {        (gameScore: PFObject!, error: NSError!) -> Void in        if error != nil {            NSLog("%@", error)        } else {            gameScore["connected"] = "yes"            gameScore.save()        }    }}
查看完整描述

3 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

简单易用的方法来检测按钮事件并执行某些操作


class youCell: UITableViewCell

{

    var yourobj : (() -> Void)? = nil


    //You can pass any kind data also.

   //var user: ((String?) -> Void)? = nil


     override func awakeFromNib()

        {

        super.awakeFromNib()

        }


 @IBAction func btnAction(sender: UIButton)

    {

        if let btnAction = self.yourobj

        {

            btnAction()

          //  user!("pass string")

        }

    }

}


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

    {

        let cell = youtableview.dequeueReusableCellWithIdentifier(identifier) as? youCell

        cell?.selectionStyle = UITableViewCellSelectionStyle.None


cell!. yourobj =

            {

                //Do whatever you want to do when the button is tapped here

                self.view.addSubview(self.someotherView)

        }


cell.user = { string in

            print(string)

        }


return cell


}


查看完整回答
反对 回复 2019-10-17
  • 3 回答
  • 0 关注
  • 377 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信