Reputation: 701
I have a HistoryTableViewController with 2 sections hooked up to a detail view controller. I want to be able to display the text and subtitle text from the HistoryTableViewController in the details view, but I want to make sure that it detects the right section. I know somewhere along the line I'll have this:
if (section == 0)
{
labelCellNum.text = Whatever was the main text in the selected table view cell
labelDueDate.text = Whatever was in the subtitle text in the selected table view cell
}
else if (section == 1)
{
labelCellNum.text = Whatever was the main text in the selected table view cell
labelDueDate.text = Whatever was in the subtitle text in the selected table view cell
}
Since I'm displaying this on a details view controller, I don't know how to get the section from the TableViewController I'm getting displayed from. I pretty much need to know how to get the section, and the main text + subtitle text.
OK Perhaps I wasn't clear enough. This code is going in my ViewDidLoad of my DetailsViewController. NOT in my tableviewcontroller.
Here is the HistoryTableviewController code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// HistoryDetailsViewController *HDV = [self.storyboard instantiateViewControllerWithIdentifier:@"HistoryDetails"];
// [self.navigationController pushViewController:HDV animated:YES];
NSLog(@"index path section--%i",[indexPath section]);
[self performSegueWithIdentifier:@"Push" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"Push"]) {
HistoryDetailsViewController *detailVC=(HistoryDetailsViewController*)segue.destinationViewController;
detailVC.labelCellNum.text = @"Cell test";
detailVC.labelDueDate.text = @"Date Test";
}
}
And below is the code of my detailsViewController:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
labelCellNum.text = @"This isnt even getting called.";
}
Upvotes: 1
Views: 4699
Reputation: 63
I assume you have a segue from the HistoryVC to the details VC. So you can pass the data across to the details VC in the prepareForSegue method in the HistoryVC. In the code fragment below, I am passing the cell title over into the new VC to set it's title. You could set other properties in the target VC, to suit your needs.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// get selected row's cell
//
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
// set desitination VC title
//
UIViewController *vc = [segue destinationViewController];
vc.navigationItem.title = cell.textLabel.text;
}
Upvotes: -1
Reputation: 3444
check this , its simple
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"index path section--%i",[indexPath section]);
[self performSegueWithIdentifier:@"Push" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"Push"]) {
DetailViewController *detailVC=(DetailViewController*)segue.destinationViewController;
detailVC.title=@"value to pass";
}
}
Upvotes: 2
Reputation: 1493
There you go!
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Selected item infos
//
int section = indexPath.section;
int row = indexPath.row;
NSLog(@"section: %d / row: %d", section, row);
}
Upvotes: 1