Wednesday, May 12, 2010

How to know in what row an accessory was clicked - TableView

I was creating a grouped tableview that had detail disclosures on some of the cells that should then open a new view and make some changes. I'm using the following code to figure out what cell the accessory is in.

The key things to remember are the "accessoryButtonTappedForRowWithIndexPath" method, and then using the switch (indexPath.section) for the section and switch (indexPath.row) for the row.

The trick I used was the copy the "switch" information directly from the method I used to build the table in the first place. I took out the logic I used, but left in a comment to help me remember what line was what. example: // Description

Happy coding!

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

ServerDescriptionView *serverDescriptionView = [[ServerDescriptionView alloc] initWithNibName:@"ServerDescriptionView" bundle:nil];

serverDescriptionView.profile = profile.agencyPolicyName;

ServerActiveServerView *serverActiveServerView = [[ServerActiveServerView alloc] initWithNibName:@"ServerActiveServerView" bundle:nil];

serverActiveServerView.profile = profile.agencyPolicyName;


switch (indexPath.section) {

case 1:

switch (indexPath.row) {

case 0:

// Description

[[self navigationController] pushViewController:serverDescriptionView animated:YES];

[serverDescriptionView release];

break;

case 1:

// Active Server

[[self navigationController] pushViewController:serverActiveServerView animated:YES];

[serverActiveServerView release];


break;

}

break;

case 2:

switch (indexPath.row) {

case 0:

// Server Power

break;

case 1:

// Unbind from the Template

break;

case 2:

// Reset UUID

break;

}

}

}

No comments: