0

我有一个分组的 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;  
}
4

3 回答 3

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
}
于 2013-04-20T03:53:40.110 回答
0

创建一个子类UIButton

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

然后实施

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

indexPath在调用superfor 这个方法之前对 the 和其他东西做一些事情。

创建按钮时,提供indexPath.

于 2013-04-20T00:46:18.593 回答
0

在你的openDocumentButtonPressedMethod:方法中,做

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

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTableView];
于 2013-04-20T00:49:38.733 回答