2014-10-08 61 views
0

我有一個6個單元格的集合視圖我怎麼讓每個單元格單擊時打開一個新的viewcontroller?Collectionview單元格打開新視圖

+0

2問題澄清:做你的6個使用一個相同的佈局?或者各有不同?如果一個單元上的水龍頭打開與其他單元相同的視圖控制器,但顯示不同的內容(如點擊縮略圖,導致大圖)?或者應該讓每個單元格導致不同的視圖控制器(如設置概覽導致不同的設置面板)? – Robert 2014-10-08 14:00:20

+0

6個單元格使用相同的佈局,並且點擊應爲每個單元點擊帶來不同的視圖。我知道你可以在不同內容的單一視圖中做到這一點,但對於我來說,作爲初學者,我認爲使用多個視圖是現在的路,現在:) – 2014-10-08 22:53:44

+0

似乎無法將我的代碼放到看到? – 2014-10-08 22:59:15

回答

1

爲了達到你的目標,你需要編輯兩個文件:

  1. 在故事板您需要在您的收藏查看添加單元格查看每個靜態細胞。只需將許多UICollectionViewCells拖到收藏視圖。對於每個單元格,當在故事板的文檔大綱中選擇一個單元格時,您需要在屬性檢查器中定義唯一的可重用標識符(表示類似名稱CellType1)。然後控制 - 從每個單元格拖動到所需的目標視圖控制器以創建Push segue(前提是您的收藏視圖與UINavigationController關聯)。

  2. 創建的UICollectionViewController一個子類,並在故事板的其指定爲集合視圖控制器的類(見身份督察)。

在你UICollectionViewController子類中實現以下方法:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    // Enter the number of static cells that are present in the Storyboard's collection view: 
    return 3; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Enter the reusable identifiers that are defined for each cell in the Storyboard's collection view: 
    NSArray *cellIdentifiers = @[@"CellType1", @"CellType2", @"CellType3"]; 
    NSInteger cellIdentifierIndex = indexPath.item; 

    // Make one identifier the default cell for edge cases (we use CellType1 here): 
    if (cellIdentifierIndex >= cellIdentifiers.count) cellIdentifierIndex = 0; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifiers[cellIdentifierIndex] forIndexPath:indexPath]; 
    // Configure the cell … 
    return cell; 
} 

我創建了一個演示應用程序顯示一個完整的實現:https://github.com/widescape/StaticCollectionViewCells

+0

非常感謝:)現在我可以繼續我的應用程序... – 2014-10-10 01:49:12

+0

不客氣! :)如果回答您的問題,請確保將我的答案標記爲已接受。 – Robert 2014-10-10 10:39:42

相關問題