2016-10-14 61 views
0

我有一個主視圖(view1)與下面的代碼。當我點擊某個部分中的單元格時,它將推向另一個視圖(視圖2),並且我希望該視圖2的標題與我點擊的視圖1的部分標題相同。傳遞tableview節標題到另一個視圖的導航欄標題

廠景

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

} 

我試圖做這樣的事情在視圖2

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

但我怎樣才能將參數部分從view1傳遞給view2?

+0

您可以顯示代碼如何推視圖2。 –

+0

@ New16好吧我剛更新了問題 – 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字符串值是否正確。 –

+0

明白了!我改成了這個'NSString * sectionTitle = [self.dataArray [tableCell.index] categoryTitle];' – SanitLee