UICollectionView使用实例(第三方类的使用及自定义UICollectionViewCell)

来源:互联网 发布:java如何实现四舍五入 编辑:程序博客网 时间:2024/05/30 23:53

首先用到了GitHub上写的AWCollectionViewDialLayout类网址是(https://github.com/awdigital/AWCollectionViewDialLayout)在使用UICollectionView的时候需要导入三个协议,分别是:

UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

#import "ViewController.h"

#import "CollectionViewCell.h"

#import "AWCollectionViewDialLayout.h"

#define WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)

#define HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

{

    NSArray *images;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    images = @[@"00.jpg",@"11.jpg",@"22.jpg",@"33.jpg",@"44.jpg",@"55.jpg",@"66.jpg"];


    AWCollectionViewDialLayout *dialLayout = [[AWCollectionViewDialLayout alloc] initWithRadius:400.0  andAngularSpacing:30.0 andCellSize:CGSizeMake(240, 100) andAlignment:WHEELALIGNMENTCENTER andItemHeight:100  andXOffset:260];

//    设置每个元素的大小

//    layout.itemSize = CGSizeMake(WIDTH/5, WIDTH/5);

//    设置两个元素的间距

//    layout.minimumInteritemSpacing = 10;

//   设置sectionInset 上左下右

//    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

//   必须超出UICollectionView的时候的滚动方向

//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    UICollectionView *showView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:dialLayout];

    showView.backgroundColor = [UIColor colorWithRed:0.910 green:1.000 blue:0.719 alpha:1.000];

    showView.showsVerticalScrollIndicator = NO;

    showView.dataSource = self;

    showView.delegate = self;

//   初始化UICollectionView的时候同时要注册cell

    [showView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:showView];

    

}

- (BOOL)prefersStatusBarHidden

{

    return YES;

}

#pragma mark -----collectionView的代理方法-----

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return images.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

//    cell.backgroundColor = [UIColor yellowColor];

    NSString *imageName = [NSString stringWithFormat:@"%@.jpg",images[indexPath.row]];

    cell.imageView.image = [UIImage imageNamed:imageName];

    cell.nameLabel.text = images[indexPath.row];

    return cell;

}

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

//{

//  CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

//    cell.backgroundColor = [UIColor colorWithRed:0.958 green:0.419 blue:1.000 alpha:1.000];

//     NSLog(@"%@%ld",cell.nameLabel.text,(long)indexPath.row);

//}

//- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

//{

//   CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

//    cell.backgroundColor = [UIColor yellowColor];

//   

//}

#pragma mark ----layout的代理方法-----

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

//{

//    return CGSizeMake(WIDTH/5, WIDTH/5);

//}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(10, 10, 10, 10);

}

@end

//自定义UICollectionViewCell

#import "CollectionViewCell.h"


@implementation CollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds)/5, CGRectGetWidth([UIScreen mainScreen].bounds)/5)];

        self.imageView.layer.cornerRadius = CGRectGetWidth([UIScreen mainScreen].bounds)/10;

        self.imageView.layer.masksToBounds = YES;

        self.imageView.layer.borderWidth = 2;

        self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;

        [self.contentView addSubview:self.imageView];

        

        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imageView.frame)-20, CGRectGetWidth(self.imageView.frame), 20)];

        self.nameLabel.textAlignment = NSTextAlignmentCenter;

        self.nameLabel.textColor = [UIColor brownColor];

        [self.contentView addSubview:self.nameLabel];

        

    }

    return self;

}


@end

最终效果图(注:可滑动)
0 0