PDA

View Full Version : Pushing to a Detail View from a Table View Cell using Xcode Storyboard




xcodeApp10
Sep 17, 2012, 11:54 AM
I have a table view inside a View Controller. I can populate all my information inside the table view. However I am a bit lost for setting up the detail views. I believe each table cell needs a segue to a each detail view but not completely sure.

Here is my code. What am I missing to accomplish the segue from the table view to the detail views?

.h

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

IBOutlet UITableView *myTable;

NSMutableArray *contentArray;
}

@property (strong, nonatomic) IBOutlet UITableView *myTable;

.m


- (void)viewDidLoad
{
contentArray = [[NSMutableArray alloc]init];
[contentArray addObject:@"Espresso"];
[contentArray addObject:@"Latte"];
[contentArray addObject:@"Capicino"];


[super viewDidLoad];
// Do any additional setup after loading the view.
}

//Table Information
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [contentArray count];
}

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

if([[contentArray objectAtIndex:indexPath.row] isEqualToString:@"EspressoViewController"])
{
EspressoViewController *espresso = [[EspressoViewController alloc] initWithNibName:@"EspressoViewController" bundle:nil];
[self.navigationController pushViewController:espresso animated:YES];
}

else if ([[contentArray objectAtIndex:indexPath.row] isEqualToString:@"Latte"])
{
LatteViewController *latte = [[LatteViewController alloc] initWithNibName:@"Latte" bundle:nil];
[self.navigationController pushViewController:latte animated:YES];
}

}

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

[self tableView:tableView didSelectRowAtIndexPath:indexPath];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"];
}

NSString *cellValue = [contentArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont systemFontOfSize:16];
cell.detailTextLabel.text = @"Hot and ready";

UIImage *image = [UIImage imageNamed:@"coffeeButton.png"];
cell.imageView.image = image;

cell.textLabel.text = [contentArray objectAtIndex:indexPath.row];
return cell;
}



jnoxx
Sep 17, 2012, 12:47 PM
Isn't it better to just create 1 Detail view, and populate that?
So make your controller, set a public variable, or create your own INIT method, create it with a property, so the detail view knows like "I'm a coffee, latte, espresso, whatever", if I am X, then show Y, if I am A, show me B. and so on.

xcodeApp10
Sep 17, 2012, 03:45 PM
I understand creating a single view that will display the different cell information. I initially thought that would be the best way. However I am still learning and not really sure how the coding would look.

I know to create a new file which I will call informationViewController.

What would be a public variable?

I know to create an @property and then to synthesis it in the .m file.

Would I put the if x then show y in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath?

Does the if x then y code look something like this?

InformationViewController *information = [[InformationViewController alloc]initWithNibName:@"InformationViewController" bundle:nil];
if ([[contentArray objectAtIndex:indexPath.row]isEqual:@"Espresso"]) {
information.coffeeInt = 0;
[information setTitle:[contentArray objectAtIndex:indexPath.row]];
}