花了好多時間去搜尋有沒有可以向右滑動的CollectionViewcontroller,結果UICollectionViewController本身就有QQ
1. 製作CollectionViewController的流程
|- 需要confirm 3個protocol
|- UICollectionViewDataSource
|- UICollectionViewDelegate
|- UICollectionViewDelegateFlowLayout
|- 實作3個delegate
//告訴collectionView要幾個section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//告訴collectionView每個section有幾的item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//將cell傳給collectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
2. 如果一開始在storyboard是拉UIViewController然後才拉UICollectionView的話需要在ViewDidLoad中加上:
- (void)viewDidLoad
{
//collectionViewDelegate
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
}
3. 如果想要scroll Direction為Horizontal記得在storyboard設定,如下圖:
4. example of implement Delegate:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
return cell;
}
沒有留言:
張貼留言