TableViewCell标题和副标题到详细视图控制器(segue帮助)

时间:2015-05-21 22:17:47

标签: ios objective-c uitableview segue detailview

我的tableview segue和详细视图控制器出了问题。我设法使用nsdictionary填充我的表格视图和标题和副标题。但是我无法将我的标题和副标题推到细节视图中。我需要我的标题去导航栏和副标题到详细视图中的标签。这是我的表格视图和详细视图的代码和屏幕截图:

#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

你可以看到我无法弄清楚如何设置我的segue。这是截图。我希望不要问如何设置segue

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

您可以获取当前选定的单元格并传递prepareForSegue:方法中的值。

-(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
}

答案 1 :(得分:0)

要清楚,您需要在选择单元格时实现委托方法,然后调用您的segue。 (假设这是您的应用程序运行的方式)

//TableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:@"ShowDetails" sender:self]; 
}

然后在转换之前,将调用prepareForSegue方法,您可以在DetailViewController上设置任何属性。

//TableViewController.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        DetailViewController *detailVC = segue.destinationViewController;
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

        detailVC.myTitle = cell.textLabel.text;
        detailVC.mySubtitle = cell.detailTextLabel.text;
    }
}

将这些属性添加到DetailViewController头文件,以将引用传递给标题和副标题。

//DetailViewController.h
@property (nonatomic, strong) NSString *myTitle;
@property (nonatomic, strong) NSString *mySubtitle;

然后在DetailViewController的viewDidLoad方法中设置导航标题和标签属性

//DetailViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = self.myTitle;
    self.myLabelName.text = self.mySubtitle;
}