手写CollectionView并调整布局
来源:互联网 发布:日立电梯调试软件 编辑:程序博客网 时间:2024/05/16 12:27
创建
if (!_colcView) int colcViewSideLength = self.view.frame.size.width - cellGap; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; _colcView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, colcViewSideLength, colcViewSideLength) collectionViewLayout:flowLayout]; _colcView.showsHorizontalScrollIndicator = NO; _colcView.showsVerticalScrollIndicator = NO; _colcView.delegate = self; _colcView.dataSource = self; _colcView.center = self.view.center; _colcView.backgroundColor = [UIColor clearColor]; [_colcView registerClass:[EPLGuestureColcViewCell class] forCellWithReuseIdentifier:CellID]; [self.view addSubview:_colcView]; }
实现协议
#pragma mark - UICollectionViewDataSource- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return _arrBtnImg.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ EPLGuestureColcViewCell *colcCell = [collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath]; if (!colcCell) { colcCell = [[EPLGuestureColcViewCell alloc]initWithFrame:CGRectZero]; } [colcCell sizeToFit]; return colcCell;}-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{ EPLGuestureColcViewCell *cell0 = (EPLGuestureColcViewCell*)cell; cell0.imgView.image = [UIImage imageNamed:_arrBtnImg[indexPath.item]]; NSNumber *numValue; if (_arrBtnValues.count) { numValue = _arrBtnValues[indexPath.item]; }else{ numValue = [NSNumber numberWithInt:-1]; } switch (numValue.intValue) { case -1: cell0.labTitle.text = NSLocalizedString(@"GUESTURE_BUTTON_CHOOSE_SHOORTKEY", nil); break; case 0: cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_PHONE", nil); break; case 1: cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_SMS", nil); break; case 2: cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_RECORDER", nil); break; case 3: cell0.labTitle.text = NSLocalizedString(@"GUESTURESETTING_ROW_CAMERA", nil); break; default: break; }}#pragma mark -- UICollectionViewDelegate- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ [collectionView deselectItemAtIndexPath:indexPath animated:YES]; NSNumber *shortcutKey; if (_arrBtnValues.count) { shortcutKey = _arrBtnValues[indexPath.item]; }else{ shortcutKey = [NSNumber numberWithInt:-1]; } EPLGuestureSettingController *settingController = [[EPLGuestureSettingController alloc]initWithStyle:UITableViewStyleGrouped]; settingController.selectedIndex = shortcutKey.integerValue; settingController.currentShortcutKeyName = _arrBtnKeys[indexPath.item]; settingController.shortcutKeyUpdated = ^(NSNumber *selectedIndex){ [_arrBtnValues replaceObjectAtIndex:indexPath.item withObject:selectedIndex]; NSDictionary *dicGuestureShortcutKeysValue = [NSDictionary dictionaryWithObjects:_arrBtnValues forKeys:_arrBtnKeys]; [EPLDataOperateKit saveGuestureShortcutKeysValue:dicGuestureShortcutKeysValue]; [_colcView reloadData]; }; [self.navigationController pushViewController:settingController animated:YES];}#pragma mark -- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ int cellEdgeLength = (collectionView.frame.size.width-2*indicatorInsets-cellGap)/2; return CGSizeMake(cellEdgeLength, cellEdgeLength);}- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ if (isEliteDevice) { return UIEdgeInsetsMake(indicatorInsets, indicatorInsets, indicatorInsets, indicatorInsets); }else{ int cellEdgeLength = (collectionView.frame.size.width-2*indicatorInsets-cellGap)/2; int indicatorInsetsForSigleItem = indicatorInsets + cellGap/2 + cellEdgeLength; return UIEdgeInsetsMake(indicatorInsetsForSigleItem/2, indicatorInsetsForSigleItem/2, indicatorInsetsForSigleItem/2, indicatorInsetsForSigleItem/2); }}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ return cellGap;}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return cellGap;}
0 0
- 手写CollectionView并调整布局
- collectionView(手写)
- collectionView 手写初始化
- iOS-CollectionView流水布局
- CollectionView自定义布局
- Collectionview自定义布局
- IOS CollectionView 线性布局
- CollectionView自定义布局
- collectionView 自动调整cell大小
- 整理 collectionView 水平自定义布局
- 整理 collectionView 瀑布流 布局
- JAVAFX纯手写布局
- 用代码手写布局
- Android 手写百分比布局
- 手写响应式布局,
- 一个简单的collectionView自定义布局
- swift CollectionView写一个多section布局
- collectionview 集合视图流式布局
- 【02单表查询】——04谓词和运算符
- IP地址转换函数
- 阻塞、非阻塞VS同步、异步
- Android中scrollview在Activity初始化过程中, 要指定滚动条位置
- UIView动画(过渡效果)的学习笔记
- 手写CollectionView并调整布局
- 阿里实习offer五面经验与总结
- Mongodb C++ Deamon 及API说明
- 状态模式(State)
- 替换或者删除Android 4.0的app
- Spring Task定时任务
- 易景地球如何加载google等在线地图数据
- git 常用命令
- lubuntu操作及桌面配置(3)