Saturday, June 8, 2013

UITableViewDelegate tableView accessoryButtonTappedForRowWithIndexPath example in Objective C (iOS).


UITableViewDelegate tableView accessoryButtonTappedForRowWithIndexPath

Tells the delegate that the user tapped the accessory (disclosure) view associated with a given row.

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

Parameters
tableView
The table-view object informing the delegate of this event.
indexPath
An index path locating the row in tableView.

Discussion of [UITableViewDelegate tableView accessoryButtonTappedForRowWithIndexPath]
The delegate usually responds to the tap on the disclosure button (the accessory view) by displaying a new view related to the selected row. This method is not called when an accessory view is set for the row at indexPath.

UITableViewDelegate tableView accessoryButtonTappedForRowWithIndexPath example.
Use the table view's cellForRowAtIndexPath: method and pass in the index path as the argument.

- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {

    Patient *patient = [fetchedResultsController_ objectAtIndexPath:indexPath];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
}

Example of [UITableViewDelegate tableView accessoryButtonTappedForRowWithIndexPath].
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    ...

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    ...
    cell.accessoryView = button;

    return cell;
}

- (void)checkButtonTapped:(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];
    }
}

UITableViewDelegate tableView accessoryButtonTappedForRowWithIndexPath example.
I created a @property in the first View Controller:

@property (nonatomic) NSInteger indexPathRow;
then I modified the code like this:

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Disclosure Tapped----> %i" ,indexPath.row);
    self.indexPathRow = indexPath.row;
    [self performSegueWithIdentifier:@"IdentityCard" sender:self];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"IdentityCard"])
    {
        NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row);
        [segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.indexPathRow]];
    }
}

End of UITableViewDelegate tableView accessoryButtonTappedForRowWithIndexPath example article.