如何将详细视图窗格标题设置为当前所选单元格的名称?

时间:2015-05-05 19:02:58

标签: ios objective-c xcode ipad master-detail

我脑子里够简单吗? 选择单元格 - 将DETAILS面板标题设置为当前所选单元格的名称。

如果您需要任何其他文件,请告诉我们。任何帮助将不胜感激!!

我的MasterViewController.h如下

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "TableViewCell.h"

@interface MasterViewController ()

@property NSMutableArray *objects;
@end

@implementation MasterViewController

- (void)awakeFromNib {
    [super awakeFromNib];
    self.clearsSelectionOnViewWillAppear = NO;
    self.preferredContentSize = CGSizeMake(320.0, 600.0);
}

- (IBAction)goHome{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Root View Controller";

    //Add button to NavigationController
    UIBarButtonItem *goHome =
    [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Home",)
                                     style:UIBarButtonItemStyleDone
                                    target:self
                                    action:@selector(goHome)];

    self.navigationItem.leftBarButtonItem = goHome;


    _subjectList = @[@"Art",
                     @"Business",
                     @"Citizenship",
                     @"Computer Science",
                     @"Dance",
                     @"Design Technology",
                     @"Drama",
                     @"English",
                     @"Food Technology",
                     @"Geography",
                     @"History",
                     @"ICT",
                     @"Languages",
                     @"Life Course",
                     @"Maths",
                     @"Media Studies",
                     @"Music",
                     @"PE",
                     @"Religious Studies",
                     @"Science",
                     @"Textiles",];


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return _subjectList.count;
}



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

    static NSString *CellIdentifier = @"TableViewCell";
    TableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    long row = [indexPath row];
    Cell.SubjectNameLabel.text = _subjectList[row];

    return Cell;


}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = self.objects[indexPath.row];
        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
        [controller setDetailItem:object];
        controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
        controller.navigationItem.leftItemsSupplementBackButton = YES;
        //THIS LINE BELOW IS WHERE I PLANNED ON SETTING THE TITLE?
        controller.navigationItem.title = selectedCell;
    }
}



@end

1 个答案:

答案 0 :(得分:4)

在详细视图控制器的/** * * Remove duplicates from an unsorted linked list. */ public class RemoveDuplicates { public static void main(String[] args) { LinkedList<String> list = new LinkedList<String>(); list.add("Apple"); list.add("Grape"); list.add("Apple"); HashSet<String> set = removeDuplicatesFromList(list); System.out.println("Removed duplicates" + set); } public static HashSet<String> removeDuplicatesFromList(LinkedList<String> list){ HashSet<String> set = new LinkedHashSet<String>(); set.addAll(list); return set; } } 文件中创建NSString属性。然后在.h中将prepareForSegue属性设置为string的名称。您将从cell数据源获取单元格标题的名称。

这就是我的意思:

tableViews中插入以下内容:

DetailViewController.h

@property (strong, nonatomic) NSString *titleName;

MasterViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showDetail"]) { NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; DetailViewController *controller = segue.destinationViewController; controller.titleName = [self.subjectList objectAtIndex:indexPath.row]; } DetailViewController.m处插入以下内容:

viewDidLoad

看一下本教程。它应该对你有所帮助:detail view controller