iOS UICollectionView 的使用

#define RGBACOLOR(r, g, b, a)   [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

#define UserCollectionViewCellID @"RRUserCollectionViewCell"
static NSString *key1 = @"RRHeaderCollectionViewCell";
static NSInteger i = 0;

@interface MyCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic ,copy) NSString *kind;
@property (nonatomic, strong) NSArray *products;

@end
@implementation MyCollectionViewController
static NSString * const reuseIdentifier = @"Cell";
- (NSArray *)products{
    if (_products == nil || i == 1) {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"CollectionViewLict.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        // 将字典转成模型
        NSMutableArray *productArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            RRHomePaceSpeedAudienceModel *p = [RRHomePaceSpeedAudienceModel userModelWithDict:dict];
            [productArray addObject:p];
        }
        
        _products = [_products arrayByAddingObjectsFromArray:productArray];
    }
    return _products;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _products = [NSMutableArray array];
    [self configUI];
    UINib *nib = [UINib nibWithNibName:@"RRUserCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:UserCollectionViewCellID];

    
    [self.collectionView registerNib:[UINib nibWithNibName:@"RRHeaderCollectionViewCell" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RRHeaderCollectionViewCell"];
    
    
    [self.collectionView registerClass:[self.collectionView.header class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:key1];
    
    
    
    
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
}
- (void)configUI{
    __weak typeof(self) weakSelf = self;
    // 添加传统的上拉刷新
    [self.collectionView addLegendFooterWithRefreshingBlock:^{
        [weakSelf loadMoreData];
        // 进入刷新状态后会自动调用这个block
    }];
}
- (void)loadMoreData{
    i = 1;
    self.products = self.products;
    [self.collectionView reloadData];
    [self endRefreshing];
    i = 0;
}
//开始刷新
- (void)beginRefreshing{
    [self.collectionView.footer beginRefreshing];
}
//结束刷新
-(void)endRefreshing{
    [self.collectionView.footer endRefreshing];
}
- (id)init{
    // 1.流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 2.每个cell的尺寸
    layout.itemSize = CGSizeMake(60, 60);
    // 3.设置cell之间的水平间距
    layout.minimumInteritemSpacing = 40;
    // 4.设置cell之间的垂直间距
    layout.minimumLineSpacing =20;
    // 5.设置四周的内边距
    layout.sectionInset = UIEdgeInsetsMake(layout.minimumLineSpacing,layout.minimumInteritemSpacing, layout.minimumLineSpacing,layout.minimumInteritemSpacing);
    return [super initWithCollectionViewLayout:layout];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource> header
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 154);//宽默认
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.products.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 1.获得cell
    RRUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:UserCollectionViewCellID forIndexPath:indexPath];
    // 2.传递模型
    cell.userModel = self.products[indexPath.item];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    [self.collectionView registerNib:[UINib nibWithNibName:@"RRHeaderCollectionViewCell" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RRHeaderCollectionViewCell"];
    UICollectionReusableView *view =  [self.collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:@"RRHeaderCollectionViewCell" forIndexPath:indexPath];
    return view;
}

#pragma mark <UICollectionViewDelegate>
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值