2014-01-23 75 views
2

我需要將UICollectionView添加到UICollectionViewCell。這似乎很容易,但我不明白。UICollectionView到UICollectionViewCell

我也不瞭解,我必須初始化UICollectionView每個UICollectionViewCell並在編程UICollectionViewDataSource每個UICollectionViewUICollectionViewDelegate

結果將是產品的集合(第一個collectionview,其中每個單元格都是產品)以及每個產品(如圖像庫(第二個集合視圖,其中每個單元格都是圖像))的圖像集合。這與Zara app相同。

第一個集合視圖看起來像這樣,只有垂直滾動。

enter image description here

當你選擇一個產品,細胞改變它們的大小,以全屏幕大小和集合視圖改變垂直滾動水平滾動。現在垂直滾動用於第二個集合視圖,即圖像庫。



二集視圖看起來像這樣

enter image description here



故事板UICollectionViewControllerUICollectionViewDataSourceUICollectionViewDelegateUICollectionViewDelegateFlowLayout實現。

ProductsCollectionViewController.h

@interface ProductsCollectionViewController : UICollectionViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> { 
    NSMutableArray *products; 
} 

ProductsCollectionViewController.m

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identifier = @"ProductCell"; 

    ProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    Product *product = [products objectAtIndex:indexPath.row]; 
    [cell setProduct:product]; 

    return cell; 
} 

產品是有一些信息,也圖像的array主要NSObject

Product.h

@interface Product : NSObject 

@property NSInteger categoryID; 
@property NSInteger productID; 
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *description; 
@property (nonatomic, strong) NSArray *images; 



我認爲我必須做的是創造新的UICollectionView編程每個ProductCollectionViewCell與產品的圖像arrayProductCollectionViewCell不承認UICollectionViewDataSourceUICollectionViewDelegate及其實施。

+1

嗯...好像你讓過於複雜。你試圖達到什麼目標? – Fogmeister

+0

你能澄清你想達到什麼嗎?你也使用故事板或以編程方式設置一切嗎? – tanzolone

+0

我需要做這樣的http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell,但與UICollectionViewController而不是UITableViewController。如果你需要我可以放一些代碼來澄清。 –

回答

2

讓我們開始有一個mainCollectionView。然後在這個集合中的每個小區創建和初始化一個新的UICollectionView,合適的地點做的就是在這下面UICollectionView

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)

電子商務代表。克每升這裏初始化MainCollectionViewCell然後MainCollectionViewCell處理邏輯,以創建一個新的UICollectionView

guard let collectionViewCell = cell as? MainCollectionViewCell else { return } 

    collectionViewCell.delegate = self 

    let dataProvider = ChildCollectionViewDataSource() 
    dataProvider.data = data[indexPath.row] as NSArray 

    let delegate = ChildCollectionViewDelegate() 

    collectionViewCell.initializeCollectionViewWithDataSource(dataProvider, delegate: delegate, forRow: indexPath.row) 

    collectionViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0 

下面是創建一個新的UICollectionView

func initializeCollectionViewWithDataSource<D: protocol<UICollectionViewDataSource>,E: protocol<UICollectionViewDelegate>>(dataSource: D, delegate :E, forRow row: Int) { 

    self.collectionViewDataSource = dataSource 

    self.collectionViewDelegate = delegate 

    let flowLayout = UICollectionViewFlowLayout() 
    flowLayout.scrollDirection = .Horizontal 

    let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: flowLayout) 
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseChildCollectionViewCellIdentifier) 
    collectionView.backgroundColor = UIColor.whiteColor() 
    collectionView.dataSource = self.collectionViewDataSource 
    collectionView.delegate = self.collectionViewDelegate 
    collectionView.tag = row 

    self.addSubview(collectionView) 

    self.collectionView = collectionView 

    collectionView.reloadData() 
} 

希望幫助上MainCollectionViewCell初始化!

我爲此做了一個例子,並放在github上。它演示了在UICollectionViewCell內使用UICollectionView

https://github.com/irfanlone/Collection-View-in-a-collection-view-cell

0

用故事板也是可能的。

啓發在Ash Furrow's article

主要的竅門是在 「willDisplayCell」 的方法。只要確保您使用的是正確的UICollectionViewCell類。

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(PNCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{ 

    if ([cell isKindOfClass:[PNCollectionViewCell class]]) { 

     [cell setCollectionViewDataSourceDelegate:self indexPath:indexPath]; 
     NSInteger index = cell.collectionView.indexPath.row; 

     CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue]; 
     [cell.collectionView setContentOffset:CGPointMake(horizontalOffset, 0)]; 
    } 

} 

有關詳細信息,你可以看到我的樣品: https://github.com/pnovales/UICollectionViewInUICollectionViewCell

相關問題