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
最终效果图(注:可滑动)- UICollectionView使用实例(第三方类的使用及自定义UICollectionViewCell)
- UICollectionView学习<2>之UICollectionViewCell的自定义
- UICollectionView和自定义UICollectionViewCell的基本用法
- UICollectionView 及 UICollectionViewCell 复用
- UICollectionViewCell的使用
- 第三方ZXing库zxing-android-embedded使用及自定义
- iOS UICollectionView 入门 05 创建自定义UICollectionViewCell
- UICollectionView 自定义使用
- UICollectionView 自定义使用
- 使用自定义的view或者引用第三方控件出错
- iOS常用的第三方及实例
- iOS常用的第三方及实例
- 一些第三方库的功能模块及使用的技术
- 80.GitHub前100第三方库及各大公司使用的第三方库
- android-友盟第三方登录,及分享的使用
- cocoapods 的安装步骤 及使用安装第三方框架
- MBProgressHUD 第三方的使用
- 第三方库的使用
- thinkphp-关于模板里 IP 地址的引用实例
- 有关static 、const等关键字概念与区别
- 奇偶分割数组
- 《编程之美》小飞的电梯调度算法
- 360笔试题--找老乡
- UICollectionView使用实例(第三方类的使用及自定义UICollectionViewCell)
- Python中的编码
- for循环大括号使用注意
- cocos2d-x 瞬时动作
- objective-c复制
- Linux网络编程入门 (转载)
- Eclipse常用快捷键
- double check locking
- Java 读取大文件