#define ScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define Screenheight ([UIScreen mainScreen].bounds.size.height)
@property (weak,nonatomic) IBOutletUICollectionView *collectionView;
@property (nonatomic,assign) BOOL isGrid;
staticNSString *scell = @"celll";
staticNSString *fcell =@"cell";
- (void)setNavtion {
// self.automaticallyAdjustsScrollViewInsets=NO;//去掉顶部留白
self.navigationItem.rightBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:@"网格"style:(UIBarButtonItemStyleDone)target:selfaction:@selector(onBtnClick:)];
}
- (void)setCollection {
_collectionView.delegate=self;
_collectionView.dataSource =self;
// 默认列表视图
_isGrid =NO;
// 注册cell
[_collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:scell];
[_collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:fcell];
}
- (void)onBtnClick:(UIBarButtonItem *)sender {
_isGrid = !_isGrid;
[self.collectionViewreloadData];
if (_isGrid) {
sender.title =@"线格";
} else {
sender.title =@"网格";
}
}
#pragma mark UICollectionViewDataSource
// cell个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return30;
}
// cell样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (_isGrid) {
//在这里注册自定义的XIBcell否则会提示找不到标示符指定的cell
UINib *nib = [UINibnibWithNibName:@"SCollectionViewCell"bundle: [NSBundlemainBundle]];
[collectionViewregisterNib:nib forCellWithReuseIdentifier:scell];
SCollectionViewCell *cell =[collectionViewdequeueReusableCellWithReuseIdentifier:scellforIndexPath:indexPath];
cell.label.text =@"姓名:90909090";
cell.labelsecond.text =@"电话:456a4dasd";
cell.iamgeView.image=[UIImageimageNamed:@"003"];
return cell;
} else {
//在这里注册自定义的XIBcell否则会提示找不到标示符指定的cell
UINib *nib = [UINibnibWithNibName:@"FCollectionViewCell"bundle: [NSBundlemainBundle]];
[collectionViewregisterNib:nib forCellWithReuseIdentifier:fcell];
FCollectionViewCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:fcellforIndexPath:indexPath];
cell.label.text =@"姓名:61465456456";
cell.labelsecond.text =@"电话:86465614";
cell.iamgeView.image=[UIImageimageNamed:@"003"];
// cell.layer.borderColor=CFBridgingRetain(([UIColor darkGrayColor]));
// cell.layer.borderWidth=0.3;
return cell;
}
}
// cell的大小 CGSizeMake
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"开始啊");
if (_isGrid) {
NSLog(@"九宫格");
returnCGSizeMake((ScreenWidth -30) / 2, (ScreenWidth -6) / 2 +40-40);
} else {
NSLog(@"条行格");
returnCGSizeMake(ScreenWidth -4, (ScreenWidth -6) / 4 +20-20);
}
}
// 四边间距 UIEdgeInsetsMake
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
if (_isGrid) {
NSLog(@"九宫格");
returnUIEdgeInsetsMake(10,10, 10,10);//分别为上、左、下、右边距
} else {
NSLog(@"条行格");
returnUIEdgeInsetsMake(2,10, 2,10);//分别为上、左、下、右边距
}
}
//设置水平间距 (同一行的cell的左右间距)
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
if (_isGrid) {
NSLog(@"九宫格");
return10;
} else {
NSLog(@"条行格");
return2;
}
}
//垂直间距 (同一列cell上下间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
if (_isGrid) {
NSLog(@"九宫格");
return10;
} else {
NSLog(@"条行格");
return2;
}
}