在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 關注
- 580 瀏覽
添加回答
舉報
0/150
提交
取消