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;

}

}

}

Tuesday, May 11, 2010

Calling methods in another class file

Now that I have my app doing quite a few things I'm starting to realize that I should have created one central place to put a bunch of functions and then call them from different views. So, I spent some time in google (because I don't have any iPhone programming books, nor do I know how to program object oriented stuff) and found a few different way to do this. Actually, they are just variations of the same thing. I'll show how I'm doing it in my code now.

In the YourFunctions.h file of the new class:

- (void) showAlert:(int)errorNumber;


In the YourFunctions.m file of the new class:

- (void) showAlert:(int)errorNumber {

UIAlertView *error = [[UIAlertView alloc] initWithTitle:[[NSString alloc] initWithFormat:@"Error: %D", errorNumber]

message:[[NSString alloc] initWithFormat:@"This is an error"]

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles: nil];

[error show];

[error release];

}


In the YourView.m file you are calling this new method from:

#import "YourFunctions.h"


// As a test I attached a button to this action.

- (IBAction)showAnAlert {

int errorNumber = 22;

YourFunctions *yourFunctions = [[YourFunctions alloc] init];

[yourFunctions showAlert:errorNumber];

}