views:

28

answers:

2

Hi there,

I have a table on my iPhone application. It works—it gets data from an array and displays it on the application. I now want to be able to add some navigation functionality to each item of the array (i.e. each item on the table).

How would I do this? My current Objective-C for the array:

- (void)viewDidLoad {
arryClientSide = [[NSArray alloc] initWithObjects:@"CSS", @"HTML", @"JavaScript", @"XML", nil];
arryServerSide = [[NSArray alloc] initWithObjects:@"Apache", @"PHP", @"SQL", nil];
self.title = @"Select a Language";
[super viewDidLoad];
}

Any help is greatly appreciated.

+1  A: 

You need to implement the table view delegate method

tableView:didDeselectRowAtIndexPath:

so that each time a user selects a row by touching it the code within this method is executed. here you simply instantiate another view controller in charge of handling the item associated to the selected row/section and push it on the navigation stack, such as

 DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
 // ...
 // Pass the selected object to the new view controller.
 detailViewController.language = [arryClientSide objectAtIndex:indexPath.row];
 [self.navigationController pushViewController:detailViewController animated:YES];
 [detailViewController release];
unforgiven
Hi there, thanks for replying.Will this code work for every item in my array? How will each item know where to go next? Would this go in my header or implementation file?Thanks a lot,Jack
Jack Griffiths
A: 

you need to implement UITableView's

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

Method inside that

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if(indexPath.row==0)
    {
        //some action
    }
    else if(indexPath.row==1)
    {
        //some action
    }

    else if(indexPath.row==2)
    {
        //some action
    }
}
mihirpmehta