自定义tableView单元格执行拖动操作(显示详细信息并执行操作而不显示详细信息

时间:2015-04-21 19:41:53

标签: objective-c swift parsing

我在自定义tableView上获取按钮时苦苦挣扎。我在自定义tableView单元格上有一个评论(评级)功能。用户可以对这些单元格进行评级。我能够获得评级,但不知道如何保存评级。

我想在自定义tableView单元格中使用一个按钮,单击该按钮将在parse.com后端保存评级。

我不想使用didSelectAtIndexPath,因为我在单元格点击上显示了单元格的详细信息。因此,提交评论的唯一方法是使用单元格内的按钮。我在Objective C中找到了一种方法,但不能在Swift中使用它。有人可以帮忙吗?

-(void)someAction:(id)sender {
UIButton *senderButton = (UIButton *)sender;
NSLog(@"current Row=%d", senderButton.tag);

PFObject *tempObject = [colorsArray objectAtIndex: senderButton.tag];
NSLog(@"%@". tempObject.objectId);
}

这就是我在tableView cellForRowAtIndexPath中引用自定义单元格的方法。

Cell.button.tag = indexPath.row // get's the button tag in sync with indexRow
Cell.button.addTarget(self, action: "someAction", forControlEvents: .TouchUpInside) // Button action function 

我甚至不确定这是否是正确的方法。我会感谢任何其他建议。

3 个答案:

答案 0 :(得分:0)

你在Swift中实现了someAction函数吗?您必须在操作字符串后面放置一个冒号,以确保您调用正确的操作函数(使用sender对象)。当你想要调用someAction(_sender: UIButton?)时,离开冒号会调用someAction()。

Cell.button.addTarget(self, action: "someAction:", forControlEvents: .TouchUpInside)

然后以与Obj-C

相同的方式编写函数
func someAction(senderButton: UIButton?) {
    println("current Row = \(senderButton.tag))
    // Save based on the tag of the button...

}

答案 1 :(得分:0)

您可以为每个按钮添加相同的目标:

Cell.button.addTarget(self, action: “someAction:”, forControlEvents: .TouchUpInside)

在此处观看,这将激活以下功能

func buttonAction(sender:UIButton!){
    var btnsendtag:UIButton = sender
    if btnsendtag.tag == 1 {            
       println(“firstButton Pressed”)
    }
}

您的操作中的会确保它将使用sender作为参数调用该函数。通过这种方式,您可以获取按钮的标签,并查看按下了哪个按钮。

答案 2 :(得分:0)


只是编辑timgcarlson的答案,因为这对我有用。 我正在使用Xcode 6.3,swift 1.2

//This is placed in cellForRowAtIndexPath
Cell.button.tag = indexPath.row
Cell.button.addTarget(self, action: "someAction:", forControlEvents: .TouchUpInside)

//This is the action function
func someAction(sender: AnyObject?) {
    let senderButton: UIButton = (sender as? UIButton)!
    println("current Row = \(senderButton.tag)")
}
Timgcarlson的回答帮我解决了这个问题。 @timgcarlson感谢您帮助我解决这个问题。请参阅:我发布了您的答案的编辑版本,以便使用Xcode 6.3 Swift 1.2的人将得到与我相同的结果。