将tableview部分标题传递给另一个视图的导航栏标题

时间:2016-10-14 03:18:13

标签: ios objective-c uitableview uitableviewsectionheader

我有一个主视图(view1),下面的代码如下。当我点击某个部分中的单元格时,它将推送到另一个视图(view2),我希望该view2的标题与我点击的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];
}

视图2

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

1 个答案:

答案 0 :(得分: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];
}