我正在try 运行一个操作,以便在表格视图单元格中按下一个按钮.下面的代码在我的表视图控制器类中.

在我的UITableViewCell类中的一个名为requestsCell的插座中,该按钮被描述为"是".

我正在使用Parse来保存数据,并希望在按下按钮时更新对象.我的ObjectID数组运行良好,单元格.对tag还将正确的数字打印到日志(log)中,但是,我无法将该数字输入到我的"已连接"函数中,以便正确运行查询.

我需要一种方法来获取信息.用于查找正确的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()
        }
    }

}

推荐答案

Swift 4 & Swift 5:

您需要为该按钮添加目标.

myButton.addTarget(self, action: #selector(connected(sender:)), for: .touchUpInside)

当然,你需要设置这个按钮的标签,因为你正在使用它.

myButton.tag = indexPath.row

可以通过将UITableViewCell子类化来实现这一点.在interface builder中使用它,在该单元上放置一个按钮,通过插座连接,就可以了.

要在已连接函数中获取标记,请执行以下操作:

@objc func connected(sender: UIButton){
    let buttonTag = sender.tag
}

Swift相关问答推荐

限制文本字段中的字符数- Swift

Swift Tree实现中的弱var

如何将泛型函数存储到变量中?

音频播放器无法播放URL音频(操作系统状态错误2003334207.)

如何获取SCNCamera正在查看的网格点(SCNNode)的局部坐标和局部法线?

Variadic泛型与Swift中的值和类型参数包

SwiftUI 组合问题:通过 AppEventsManager 在 ViewModel 之间共享数据

TimeZone 背后的目的

macOS 的窗口框架中使用的真实类型和真实类型是什么?

关于变量的视图的 SwiftUI 生命周期

用逻辑运算符保护让

如果 Swift 无法分配内存会怎样?

Swift:创建用于调用 C 函数的指针数组

在 Swift 5.5 中编写同步和异步函数

SwiftUI:从存储在 observableObject 中的数组中删除对象时查看崩溃

如何在 Swift 中返回 Task 中定义的变量

在 Swift 4 中实现自定义解码器

在 UItextfield 的右视图上添加一个按钮,文本不应与按钮重叠

在 Swift 中以编程方式更新约束的常量属性?

如何快速将弹出框调整为表格视图中内容的大小?