检测在分组的UITableView中单击了哪个按钮

时间:2013-04-20 00:18:24

标签: ios objective-c xcode uitableview

我有一个分组的UITableView(这个有4个部分),每个都有几行。我已经以编程方式在每个单元格中创建了2个按钮。

这就是我创建UITableViewCell的方法。在这段代码中,我试图检测按下的按钮的indexPath.row和indexPath.section并将其传递给方法。我怎么能这样做?

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell ;//= [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}


NSString *docTitle = [currentDocument objectForKey:@"displayname"];

UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+5, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(cellView.frame.origin.x + 5, cellView.frame.origin.y + 5, cellView.frame.size.width - 10, 25)];
cellTitle.backgroundColor = [UIColor clearColor];
cellTitle.text = docTitle;
[cellView addSubview:cellTitle];

[cell.contentView addSubview:cellView];

UIButton *viewDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewDocumentButton setTitle:@"View Online" forState:UIControlStateNormal];
viewDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5,       cellTitle.frame.origin.y + cellTitle.frame.size.height + 5, 150, 35);
[viewDocumentButton addTarget:self action:@selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[viewDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:viewDocumentButton];

UIButton *downloadDocumentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[downloadDocumentButton setTitle:@"Download Document" forState:UIControlStateNormal];
downloadDocumentButton.frame = CGRectMake(cellView.frame.origin.x + 5, viewDocumentButton.frame.origin.y + viewDocumentButton.frame.size.height + 5, 150, 35);
[downloadDocumentButton addTarget:self action:@selector(openDocumentButtonPressedMethod:) forControlEvents:UIControlEventTouchDown];
[downloadDocumentButton setTag:indexPath.row];
[cell.contentView addSubview:downloadDocumentButton];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;  
}

3 个答案:

答案 0 :(得分:4)

另外,你可以......

- (IBAction)openDocumentButtonPressedMethod:(id)sender {
    UIButton *button = (UIButton *)sender;
    UIView *contentView = button.superview;
    UITableViewCell *cell = (UITableViewCell *)contentView.superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // do something with the indexPath
}

如果您想使用相同的方法来处理两个按钮事件,那么您可以使用按钮上的标记来区分这两个按钮。

if (button.tag == 1) {
    // do something with the indexPath view related
}
else {
    // do something with the indexPath download related
}

答案 1 :(得分:0)

创建UIButton

的子类
@interface MyButton : UIButton 
{ /* indexPath, other stuff */ }
@end

然后实施

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event

在为此方法调用indexPath之前,使用super和其他内容执行某些操作。

创建按钮时,请提供indexPath

答案 2 :(得分:0)

openDocumentButtonPressedMethod:方法中,执行

CGPoint pointInTableView = [self.tableView convertPoint:sender.bounds.origin fromView:sender];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTableView];