I have implemented the accessory view of a UITableViewCell as a button, in the
button’s selector method i have the following code
- (UIButton *) makeDetailDisclosureButton
{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage* image = [UIImage imageNamed:@"custom.png"];
[button setImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget: self
action: @selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
return ( button );
}
- (void) accessoryButtonTapped: (UIControl *)button withEvent:(UIEvent *) event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if (indexPath != nil)
{
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}
}
However sometimes the wrong row is being selected, ie if I tap on row 0 in the table i get
row = 1 actually being selected. The code above is quite well known as a solution for
a custom accessory view but it is proving unreliable. Is there something else i need to do here?
Try this,
The idea here is pretty basic. Button’s superview should give me
cell.contentViewand it’s superview is thecell. There is lovely helper methodindexPathForCell:which will give us the index path which we can use to pass to the delegate method.