标签: collectionview
2015-03-31 15:45 18166人阅读 收藏 举报
这个控件,看起来与UITableView有点像,而且基本的用法也很相像哦!!!
我们来看看API:
- #pragma mark - UICollectionViewDataSource
-
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 3;
- }
-
-
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return 10;
- }
-
-
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
- cell.backgroundColor = [UIColor redColor];
- cell.textLabel.text = [NSString stringWithFormat:@"(%ld %ld)", indexPath.section, indexPath.row];
-
- return cell;
- }
看看直线布局的API:
- #pragma mark - UICollectionViewDelegateFlowLayout
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3 - 10);
- }
-
-
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- return UIEdgeInsetsMake(5, 5, 5, 5);
- }
-
-
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 10;
- }
-
-
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
- return 10;
- }
-
-
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
- return CGSizeMake(self.view.frame.size.width, 40);
- }
-
-
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
- return CGSizeMake(self.view.frame.size.width, 40);
- }
如果是固定的,可以使用全局属性:
- NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewFlowLayout : UICollectionViewLayout
-
- @property (nonatomic) CGFloat minimumLineSpacing;
- @property (nonatomic) CGFloat minimumInteritemSpacing;
- @property (nonatomic) CGSize itemSize;
- @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0);
- @property (nonatomic) UICollectionViewScrollDirection scrollDirection;
- @property (nonatomic) CGSize headerReferenceSize;
- @property (nonatomic) CGSize footerReferenceSize;
- @property (nonatomic) UIEdgeInsets sectionInset;
-
- @end
常用到的代理方法:
- #pragma mark - UICollectionViewDelegate
-
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
-
-
- - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
-
-
- - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
-
-
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
-
-
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
-
-
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
-
-
- - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }