Brad-ley
Brad-ley

Reputation: 43

Tableview push to different view

I have this code which pushes to a new view when a cell is pressed. This new view changes its title based on the name of the cell pressed. However the view is the same across all cells, if I change something under one cell it changes the view for each other cell. How can I do this so each view is different for each cell without creating loads of views; which would be impractical. Thank you.

Push Code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

ACollection *newView = [[ACollection alloc] init];
newView.template = [[Global collections] objectAtIndex:indexPath.row];

newView.theTitle = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[self.navigationController pushViewController:newView animated:YES];
}

Upvotes: 0

Views: 318

Answers (2)

Mathew Varghese
Mathew Varghese

Reputation: 4527

Well, consider now we have the string in the array.(Number of array items is equal to the number of cells. Or We can say that you are loading the details from the array.).

First of all, in the didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
         // Here we got the name of the item we are currently going to load
         NSString *item_Name   =   [contentArray objectAtIndex: indexPath.row];
         ViewController *yourViewController   =  [[ViewController alloc] initWithNIBName: @"ViewController" bundle: nil];
         yourViewController.navigationItem.title  =  item_Name;
         [self.navigationController pushViewController: yourViewController animated: YES];
} 

or like lukya said you can use

NSString *item_Name   =   [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;

if you are going to set the title label of the cell itself as the title of the detail view.

Hope this helps.

Upvotes: 0

Swapnil Luktuke
Swapnil Luktuke

Reputation: 10475

theTitle seems to be your local variable which you are setting on cell selection. However, navigation controller sets the view controller's title in navigation item. So, you can set AVCollections title like tis:

newView.navigationItem.title = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;

during cell selection.

Upvotes: 1

Related Questions