14

I have a pretty big issue. I am trying to create a favorite-button on every UITableViewCell in a UITableView. That works very good, and I currently have an action and selector performed when pressed.

accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 15, 15);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = accessory;

And selector:

- (void) didTapStar {
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:/* indexPath? */];

    accessory = [UIButton buttonWithType:UIButtonTypeCustom];
    [accessory setImage:[UIImage imageNamed:@"stared.png"] forState:UIControlStateNormal];
    accessory.frame = CGRectMake(0, 0, 26, 26);
    accessory.userInteractionEnabled = YES;
    [accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchDown];
    newCell.accessoryView = accessory;
}

Now, here's the problem: I want to know what row the accessory that was pressed belongs to. How can I do this?

Thank you :)

Emil
  • 7,051
  • 17
  • 72
  • 131

3 Answers3

20

I would use the following code:

- (void)didTapStar:(id)sender {
  NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)[sender superview]];
}
Matteo Alessani
  • 9,864
  • 4
  • 37
  • 56
Bas Dirkse
  • 216
  • 1
  • 2
16

Change your action around a bit.

- (void)action:(id)sender forEvent:(UIEvent *)event

Inside that method:

NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];

That should get you the row's index path.

Giao
  • 13,751
  • 2
  • 22
  • 18
  • 1
    What is the role of `anyObject` here ? what does it do ? – Illep Oct 02 '11 at 17:25
  • @Giao I'm just curious, what does `anyObject` do? – Emil Nov 16 '12 at 13:51
  • 1
    @lllep @Emil `anyObject` is an instance method in `NSSet`, this method simple return any single object in the set. In this example, `[[[event touchesForView:sender] anyObject]` returns a `UITouch` object. – Jeffery Li Nov 25 '12 at 15:13
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath {   
    // To set custom cell accessory                     
    UIImage *image = [UIImage   imageNamed:@"white_arrow_right.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(cell.frame.size.width  - image.size.width, cell.frame.size.height - image.size.height, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(accessoryButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
}     

- (void)accessoryButtonTapped:(id)sender event:(id)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];
    }
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{                
    DetailViewController *detailView =[[DetailViewController alloc]init];
    [self.navigationController pushViewController:detailView animated:YES];
}
Emil
  • 7,051
  • 17
  • 72
  • 131