addTarget:action:forControlEvents: from a UITableViewCell



我有一个UITableViewCell与3复选标记按钮。每个复选标记按钮都是一个自定义UIButton(实际上只是图像),并且在自定义UITableViewCell子类中有一个outlet。在cellForRowAtIndexPath方法中,I

[cell.firstcheckmark addTarget:self action:@selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.secondcheckmark addTarget:self action:@selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.thirdcheckmark addTarget:self action:@selector(checkmarkPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)checkmarkPressed:(id)sender {
   // how do I get which checkmark was pressed?
}

我不确定如何从发送者对象中获得按下的按钮。其他时候我可以使用按钮的标题,但在这个例子中,所有按钮都是一样的,除了它们的outlet。我怎么知道哪个按钮被按下了?或者它们是否都需要调用不同的方法,然后在这三个方法中的每一个中,我都可以通过从这三个方法中调用相同的modelUpdateMethod:来更新我的模型?谢谢。

您可以通过它们的标签来识别它们:

cell.firstcheckmark.tag = 0;

- (void)checkmarkPressed:(id)sender {
   UIButton *button = (UIButton*)sender;
   if(button.tag == 0)
   {
      ...
   }
   ...
}

我想你也可以用:

UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];

它可以得到按钮在哪个单元格

最新更新