codeetcetera
codeetcetera

Reputation: 3203

UITableViewCell indexPathForRowAtPoint always returns nil

I'm trying to use a custom disclosure button as per Apple's example. I'm doing the following

- (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)
    {

    }


}

The problem is that index path is always nil. Every time without fail. Has anyone else come across this?

EDIT This is how I'm adding the button

 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;

Upvotes: 2

Views: 1993

Answers (1)

user467105
user467105

Reputation:

The call to indexPathForRowAtPoint: can return nil if self.tableView is nil.

Make sure self.tableView is set (eg. if it's an IBOutlet, make sure it is connected to the table view in the xib).

Upvotes: 3

Related Questions