在UITableView中检测哪个UIButton被按下我有一个UITableView有5个UITableViewCells..每个单元格包含一个UIButton其设置如下:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell autorelelase];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
[button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:1];
[cell.contentView addSubview:button];
[button release];
}
UIButton *button = (UIButton *)[cell viewWithTag:1];
[button setTitle:@"Edit" forState:UIControlStateNormal];
return cell;}我的问题是:在buttonPressedAction:方法,如何知道已按哪个按钮。我考虑过使用标签,但我不确定这是最好的路线。我希望能以某种方式标记indexPath进入控制室。- (void)buttonPressedAction:(id)sender{
UIButton *button = (UIButton *)sender;
// how do I know which button sent this message?
// processing button press for this row requires an indexPath. }这样做的标准方法是什么?
3 回答
大话西游666
TA贡献1817条经验 获得超14个赞
[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- (void)checkButtonTapped:(id)sender{ CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; if (indexPath != nil) { ... }}
HUWWW
TA贡献1874条经验 获得超12个赞
- (IBAction)buttonTappedAction:(id)sender{ CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; ...}
- 3 回答
- 0 关注
- 551 浏览
添加回答
举报
0/150
提交
取消