2016-10-14 2 views
0

У меня есть основной вид (view1) с этим ниже кодом. Когда я нажимаю ячейку в секции, она будет нажимать на другое представление (view2), и я хочу, чтобы заголовок этого представления был таким же, как и заголовок раздела view1, который я использую.Передача заголовка раздела таблицы в заголовок навигационной панели другого вида

view1

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)]; 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:headerView.frame]; 
    imageView.image = [UIImage imageNamed:@"bg_blue.png"]; 
    [headerView addSubview:imageView]; 

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, kScreenWidth, 20)]; 
    title.textColor = [UIColor whiteColor]; 
    CategoryListData *data = self.dataArray[section]; 
    title.text = data.categoryTitle; 
    [headerView addSubview:title]; 

    return headerView; 
} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    MainHeaderTableViewCell *tableCell = (MainHeaderTableViewCell *)collectionView.superview.superview; 
    CategoryListData *data = self.dataArray[tableCell.index]; 
    CategoryDetailData *detailData = data.categoryList[indexPath.row]; 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    View2 *vc = [storyboard instantiateViewControllerWithIdentifier:@"View2"]; 
    vc.detailData = detailData; 
    vc.hidesBottomBarWhenPushed = YES; 
    [self.navigationController pushViewController:vc animated:YES]; 
} 

view2

- (void)initNavigationTitle { 
    [self setupNavigationTitle:self.detailData.title backButtonWithAnimated:YES]; // this line need to be replaced with section name from view1 

} 

Я пытаюсь сделать что-то подобное в View2

CategoryListData *data = self.dataArray[section]; 
title.text = data.categoryTitle; 
[self setupNavigationTitle:title.text backButtonWithAnimated:YES]; 

Но как я могу передать раздел параметров из view1 в view2?

+0

Можете ли вы показать код, как вы нажимаете view2. –

+0

@ New16 ok Я только что обновил вопрос – SanitLee

+0

Итак, у вас используется просмотр коллекции в ячейке tableview? – ashmi123

ответ

1
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    MainHeaderTableViewCell *tableCell = (MainHeaderTableViewCell *)collectionView.superview.superview; 
    CategoryListData *data = self.dataArray[tableCell.index]; 
    CategoryDetailData *detailData = data.categoryList[indexPath.row]; 

    NSString *sectionTitle = [self.dataArray[tableCell.index] categoryTitle]; 

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    View2 *vc = [storyboard instantiateViewControllerWithIdentifier:@"View2"]; 
    vc.detailData = detailData; 
    vc.title = sectionTitle; 
    vc.hidesBottomBarWhenPushed = YES; 
    [self.navigationController pushViewController:vc animated:YES]; 
} 
+0

Спасибо, но есть проблема, когда я нажимаю другую секцию, а затем переходим к view2, она по-прежнему показывает название из моего первого нажатия. Как я могу это обновить? – SanitLee

+1

Этого не должно быть. Потому что вы создаете новый viewController каждый раз, когда вы выбираете ячейку. Отладка, чтобы проверить правильность значения параметра sectionTitle String. –

+0

получил! я изменил этот раздел 'NSString * sectionTitle = [self.dataArray [tableCell.index] categoryTitle];' – SanitLee

Смежные вопросы