Saturday, June 8, 2013

UITableView tableView accessoryButtonTappedForRowWithIndexPath example in Objective C (iOS).


UITableView 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 of [UITableView tableView accessoryButtonTappedForRowWithIndexPath]
tableView
The table-view object informing the delegate of this event.
indexPath
An index path locating the row in tableView.

Discussion of [UITableView 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.

UITableView 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 [UITableView 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];
    }
}

UITableView 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 UITableView tableView accessoryButtonTappedForRowWithIndexPath example article.