Reputation: 1773
I am making a simple application in an attempt to familiarise myself with storyboards and their use. The application loads up with an initial Table View which is populated via my AppDelegate.m didFinishLaunchingWithOptions method. This works fine so have no problem with that.
The next stage is depending on which cell is tapped in my table I want a view with a different image to be shown. In a previous life I would have created a new .xib file for everyone of these and possibly would have ended up with 100 .xib files each with a different image on. I would have then used the following code to show that view;
if (indexPath.row == 0)
{
UIViewController *controller = [[UIViewController alloc]initWithNibName:@"AlertViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else if (indexPath.row == 1)
{
// Show a different view
}
I imagine that this is totally the wrong way of doing it as it requires a lot of overhead and the creation nibs of however many views I need. I have been told that I should just have one additional view and to just pass the relevant image to that view depending on which row has been selected.
I have no idea how to do this though. I have created an additional view controller in my storyboard which is linked to the table view. How would I go about passing in a different image depending on which row in the table is selected?
Upvotes: 0
Views: 801
Reputation: 4276
Suppose you have an array which contains the elements you are displaying in your table. You might have an array (called myArray) with string items like this... item1, item2, item3, item4
Currently, these are just being displayed on your tableview.
Now, if you instead create an array of objects (or structs) which contain both the display name and the name of the graphic you want to display
(item1, graphic1), (item2, graphic2), (item3, graphic3), (item4, graphic4)
Now in your didSelectRow method, you can get the item that they selected
selectedItem = [myArray objectAtIndex:indexPath.row];
And from that you can get the name of the graphic
selectedGraphic = selectedItem.graphicName;
And now you can create your new view and give it the graphic
UIViewController *controller = [[UIViewController alloc] ....];
controller.graphic = [UIImage imageNamed:selectedGraphic];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NOTE : I realise controller.graphic doesn't exist - I've just put that in to show you where you would set your image. How you actually do this depends on your other objects and controllers, but hopefully this will give you an idea. Same goes for the other code snippets.
Upvotes: 6
Reputation: 655
if (indexPath.row == 0)
{
UIViewController *controller = [[UIViewController alloc]initWithNibName:@"AlertViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.mylable1.text=@"xyz";
controller.mylable2.text=@"abc";
.
.
.
[self.navigationController pushViewController:controller animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
do like this...
Upvotes: 0