在iOS7中,IBAction始终返回0点击索引

时间:2013-11-06 14:41:44

标签: objective-c uitableview row nsindexpath

以下是我所拥有的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MainCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    UIButton *myButton = (UIButton *)[cell viewWithTag:999999];
    // setting image to button...
    return cell;    
}

在IB中,我已对此按钮设置了操作...

- (IBAction)clickedCellAction:(id)sender {

    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
    NSIndexPath *clickedButtonPath = [mainTableView indexPathForCell:clickedCell];

    NSLog(@"row==%d", clickedButtonPath.row);
}

现在,当我运行此功能时,我总是在iOS 7中获得row==0

然而,它在iOS6中提供了正确的行==。

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

以下是我用过的......

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:mainTableView];
NSIndexPath *indexPath = [mainTableView indexPathForRowAtPoint:buttonPosition];
NSLog(@"row index---%d", indexPath.row);
相关问题