iOS Swift Button表格视图单元格中的动作

iOS Swift Button表格视图单元格中的动作,第1张

概述我正在尝试为在表格视图单元格中按下的按钮运行一个动作.以下代码在我的表视图控制器类中.该按钮在我的UITableViewCell类别中的一个插座中被描述为“是”,名为requestsCell.我使用解析来保存数据,并希望在按下按钮时更新对象.我的objectIds数组工作正常,cell.yes.tag也会将正确的数字打印到日志中,但是,为了正确运行我的查询,我无法将该数字输入到我的“已连接”函数中 我正在尝试为在表格视图单元格中按下的按钮运行一个动作.以下代码在我的表视图控制器类中.该按钮在我的UItableVIEwCell类别中的一个插座中被描述为“是”,名为requestsCell.我使用解析来保存数据,并希望在按下按钮时更新对象.我的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()        }    }}
解决方法 您需要添加该按钮的目标.
mybutton.addTarget(self,action: "connected:",forControlEvents: .touchUpInsIDe)

当然,您需要设置该按钮的标签,因为您正在使用该按钮.

mybutton.tag = indexPath.row

您可以通过将UItableVIEwCell进行子类化来实现此目的.在界面构建器中使用它,在该单元格上放一个按钮,通过插座连接到那里.

编辑:
要在连接的功能中获取标签:

func connected(sender: UIbutton){    let buttonTag = sender.tag}

EDIT2:

这个答案是针对swift 1.2提供的,正如评论中所提到的那样,swift 2.2的语法有所不同.

mybutton.addTarget(self,action: #selector(Classname.Functionname(_:),forControlEvents: .touchUpInsIDe)

EDIT3:

更新为Swift 3

mybutton.addTarget(self,action: #selector(Classname.Functionname.buttonTapped),for: .touchUpInsIDe)
总结

以上是内存溢出为你收集整理的iOS Swift Button表格视图单元格中的动作全部内容,希望文章能够帮你解决iOS Swift Button表格视图单元格中的动作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1112035.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存