【问题标题】:TableViewCell title and subtitle to detail view controller (segue help)UITableViewCell 标题和副标题到详细视图控制器(segue 帮助)
【发布时间】:2015-08-03 19:15:56
【问题描述】:

我的 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不要太多

【问题讨论】:

    标签: ios objective-c uitableview segue detailview


    【解决方案1】:

    您可以获取当前选中的单元格并在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
    }
    

    【讨论】:

      【解决方案2】:

      需要明确的是,您需要在选择单元格时实现委托方法,然后调用您的 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-09
        • 1970-01-01
        相关资源
        最近更新 更多