2

我正在尝试按照 Apple 的示例使用自定义披露按钮。我正在做以下事情

- (void) customButtonTapped:(UIButton *)sender event: (id) event{

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"index path %@", indexPath);
    if (indexPath != nil)
    {

    }


}

问题是索引路径始终为零。每次都无懈可击。有没有其他人遇到过这个?

编辑 这就是我添加按钮的方式

 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
 UIImage *image = [UIImage imageNamed:@"Gear"];
 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
 button.frame = frame;
 [button setBackgroundImage:image forState:UIControlStateNormal];
 button.backgroundColor = [UIColor clearColor];
 [button addTarget:self action:@selector(customButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
 cell.accessoryView = button;
4

1 回答 1

3

调用indexPathForRowAtPoint:可以返回nilifself.tableViewnil

确保self.tableView已设置(例如,如果它是 IBOutlet,请确保它已连接到 xib 中的表视图)。

于 2013-01-25T16:49:18.593 回答