Search code examples
iosobjective-cuitableviewseguedetailview

TableViewCell title and subtitle to detail view controller (segue help)


i got a problem with my tableview segue and the detail view controller. i managed to populate my table view with titles and subtitles with nsdictionary. however i could not push my title and subtitle to the detail view. i need my title to go on the navigation bar and the subtitle to a label in the detail view. here is the code and the screenshots of my table view and the detail view:

#import "TableViewController.h"
#import "DetailViewController.h"

@interface TableViewController (){

    NSDictionary *sarkilar;
    NSArray *sarkilarSectionTitles;
    NSArray *sarkilarIndexTitles;

}

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [[self navigationItem] setBackBarButtonItem:newButton];

    sarkilar = @{
                 @"A" : @[@{@"title":@"Alayına İsyan",@"subtitle":@"Seslendiren: Mustafa Sandal"},
                        @{@"title":@"Ardindan",@"subtitle":@"Seslendiren: Sinasi Gurel"}],
                 @"B" : @[@{@"title":@"Birak Gitsin",@"subtitle":@"Seslendiren: Tarkan"},
                        @{@"title":@"Buralar",@"subtitle":@"Seslendiren: Duman"}],
                 @"C" : @[@{@"title":@"Cephaneler",@"subtitle":@"Seslendiren: Burak Kut"},
                          @{@"title":@"Cari Acik",@"subtitle":@"Seslendiren: Kristal"}],
                 };


    sarkilarSectionTitles = [[sarkilar allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    sarkilarIndexTitles = @[@"A", @"B", @"C",@"Ç", @"D", @"E", @"F", @"G", @"H", @"I",@"İ", @"J", @"K", @"L", @"M", @"N", @"O", @"Ö", @"P", @"R", @"S",@"Ş", @"T", @"U",@"Ü", @"V", @"Y", @"Z"];



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [sarkilarSectionTitles count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:section];
    NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
    return [sectionSarkilar count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [sarkilarSectionTitles objectAtIndex:section];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSString *sectionTitle = [sarkilarSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionSarkilar = [sarkilar objectForKey:sectionTitle];
    NSDictionary *dict = [sectionSarkilar objectAtIndex:indexPath.row];
    NSString *title = [dict objectForKey:@"title"];
    NSString *subtitle = [dict objectForKey:@"subtitle"];
    cell.textLabel.text = title;
    cell.detailTextLabel.text = subtitle;

}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    //  return animalSectionTitles;
    return sarkilarIndexTitles;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [sarkilarSectionTitles indexOfObject:title];
}
//-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    
//    if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
//        DetailViewController *detailView = [segue destinationViewController];
//        
//        NSIndexPath *myindexpath = [self.tableView indexPathForSelectedRow];
//        
//        int row = [myindexpath row];
//        detailView.DetailModal = @[_title[row], subtitle[row],];
//    }
//    
//    
//    
//}
//
@end

as you can see i couldn't figure out how to set my segue up. and here are the screenshots. i hope it is not too much to ask how to set up the segue

enter image description here enter image description here


Solution

  • You can get the currently selected cell and pass the values in the prepareForSegue: method.

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
         UIViewController *destinationViewController = segue.destinationViewController;
    
         UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
    
         destinationViewController.title = selectedCell.textLabel.text;
         //Add code to set label to selectedCell.detailTextLabel.text
    }