2013-09-28 56 views
8

我有一個UICollectionView控制器嵌入導航控制器內。 collectionView列出了項目,每個單元都應該延伸到ProjectDetail屏幕。iOS:故事板CollectionView不會被觸發

我根本無法得到繼續觸發。如果我簡單地在導航欄上放置一個按鈕並將細節連接到細節,它就可以工作。但從我的CollectionView單元觸發不。

這裏是故事板的樣子:我做http://cl.ly/RfcM有從CollectionViewCell掛在ProjectDetailViewController

一個賽格瑞這裏是我的ProjectDetailViewController內的相關代碼:

@interface ProjectCollectionViewController() { 
    NSArray *feedPhotos; 
    Projects *projects; 
} 

@end 

@implementation ProjectCollectionViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.collectionView registerClass:[FeedViewCell class] forCellWithReuseIdentifier:@"cell"]; 
    [self loadData]; 

} 

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"selected %d", indexPath.row); 
    Project *project = [projects getProject:indexPath.row]; 
    NSLog(@"project = %@", project); 
} 

- (void)loadData { 

    [self.projectLoader loadFeed:self.username 
         onSuccess:^(Projects *loadedProjects) { 
          NSLog(@"view did load on success : projects %@", loadedProjects); 
          projects = loadedProjects; 

          [self.collectionView reloadData]; 
         } 
         onFailure:^(NSError *error) { 
          [self handleConnectionError:error]; 
         }]; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return projects.count; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identifier = @"cell"; 
    FeedViewCell *cell = (FeedViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 
    cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0]; 
    UIImageView *cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    Project *project = [projects getProject:indexPath.row]; 
    NSString *imageUrl = [project coverPhotoUrl:200 forHeight:200]; 
    NSLog(@"imageurl =>%@", imageUrl); 
    if (imageUrl) { 
     [cellImageView setImageWithURL:[NSURL URLWithString:imageUrl]]; 
    } 
    [cell addSubview:cellImageView]; 
    cell.imageView = cellImageView; 
    return cell; 
} 

我猜問題在於我如何將單元連接到CollectionView。

任何幫助將不勝感激!

回答

21

你不能直接從細胞在故事板創建塞格斯因爲的CollectionView是通過動態填充數據源。您應該使用collectionView:didSelectItemAtIndexPath:並使用performSegueWithIdentifier:sender:以編程方式執行segue。是這樣的:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { 
    [self performSegueWithIdentifier:@"MySegueIdentifier" sender:self]; 
} 

其中MySegueIdentifier是在故事板中定義的SEGUE的標識符。

+0

那麼有人能告訴我發生了什麼變化嗎?因爲在這裏http://www.appcoda.com/ios-collection-view-tutorial/我們可以清楚地看到連接是在「Storyboard」中進行的,並且它可以工作。 – Pahnev

+0

此答案已過時。當然,你可以直接在Storyboard中創建segues。 –

2

您是否在Triggered Seguesselection上製作了CollectionView Cell的連接?

您也可以觸發編程方式使用 [self performSegueWithIdentifier:@"segueIdentifier" sender:nil]

一個賽格瑞在

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

3

TLDR:對於一個故事板,不要​​調用registerClass:forCellWithReuseIdentifier :.它改寫了故事板的電池(包括塞格斯是如何處理的)設置: How to set a UILabel in UICollectionViewCell

簡要設置

  • 用故事板
  • 使用Xcode模板創建一個新的集合視圖控制器, 將其設置爲UICollectionViewController的子類。
  • 最初使用默認的UICollectionViewCell,以編程方式添加UILabel 。

生成UICollectionViewController代碼登記在viewDidLoad中的細胞:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; 

第一期: 的prepareForSegue:發件人:事件不觸發,這給我帶來了這個答案。 我實現了UICollectionViewDelegate和collectionView:didSelectItemAtIndexPath:event,然後以編程方式調用了segue。 這解決了我的第一個問題。

第二期:我切換到包含一個標籤的自定義單元格。將所有東西都鉤住後,單元格標籤不顯示。 經過一番挖掘,我找到了解決方案頂部鏈接中的解決方案。

第三期和解決方案:我刪除了registerClass:forCellWithReuseIdentifier:行。當我運行我的應用程序時,標籤顯示正確,但是當我點擊一個單元格時,它將兩次調用prepareForSegue:sender事件。通過刪除registerClass:forCellWithReuseIdentifier行,該單元直接處理單元格觸摸,而不需要委託方法。這就是我期望的故事板的工作原理。我刪除了collectView:didSelectItemAtIndexPath:event,它解決了prepareForSegue:sender:的雙重觸發。 如果您正在使用故事板,請勿註冊單元類。它會覆蓋故事板設置的內容。

+0

@Claude你知道爲什麼使用collectionView:didSelectItemAtIndePath:覆蓋storyboard設置的內容嗎? – preynolds

0

類似問題的等價Swift代碼。

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    self.performSegueWithIdentifier(@"TargetSegway", sender: self) 
} 

確保,在情況下,如果你的電池還有其他重疊的看法,「用戶交互啓用」是選中(你可以找到這個選項,在屬性檢查器中查看/交互)。否則,您的Tap Gesture將被重疊視圖佔用,didSelectItemAtIndexPath可能不會被調用。