日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于collectionview布局的坑

發(fā)布時間:2023/12/2 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于collectionview布局的坑 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

不知道寫了多少次collectionview,步了很多坑,現(xiàn)在看來雖然達到了自己想要的結(jié)果,卻不知道其中所以然。還是總結(jié)一下,免得再走彎路;

場景是這樣的,我要定制一個顯示選擇圖片的排列,想要實現(xiàn)橫向排列4個,等間距,多了折行顯示的效果,正確的做法是這樣的;

- (void)viewDidLoad {[super viewDidLoad];self.navigationController.navigationBar.translucent = NO;if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {self.automaticallyAdjustsScrollViewInsets = NO;}self.view.backgroundColor = [UIColor purpleColor];self.photoArray = @[[UIImage imageNamed:@"cycle3"],[UIImage imageNamed:@"cycle4"],[UIImage imageNamed:@"cycle2"],[UIImage imageNamed:@"cycle3"],[UIImage imageNamed:@"cycle4"],].mutableCopy;[self.view addSubview:self.pickPhotoCollection]; }-(UICollectionView *)pickPhotoCollection{if (!_pickPhotoCollection) {UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];layout.scrollDirection = UICollectionViewScrollDirectionVertical;_pickPhotoCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 200, kScreenWidth, (self.photoArray.count/4 +1)*80) collectionViewLayout:layout];_pickPhotoCollection.delegate = self;_pickPhotoCollection.dataSource = self;_pickPhotoCollection.backgroundColor = RGBACOLOR(240, 240, 240, 1);_pickPhotoCollection.showsHorizontalScrollIndicator = NO;[_pickPhotoCollection registerClass:[XJPickPhotoCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XJPickPhotoCollectionViewCell class])];}return _pickPhotoCollection; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{return 1; }-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{return self.photoArray.count + 1 > 9 ? 9 : self.photoArray.count +1; }-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{XJPickPhotoCollectionViewCell *cell = (XJPickPhotoCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XJPickPhotoCollectionViewCell class]) forIndexPath:indexPath];//判斷圖片的顯示方式 如果不是9張圖 則顯示可以繼續(xù)添加if (self.photoArray.count == 9) {cell.photoImageView.image = self.photoArray[indexPath.row];return cell;}if (indexPath.row == self.photoArray.count) {cell.photoImageView.image = [UIImage imageNamed:@"addPhoto"];}else{cell.photoImageView.image = self.photoArray[indexPath.row];}return cell; }-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{return CGSizeMake(WidthScale(70), WidthScale(70)); } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ // return 2;return (kScreen_width - WidthScale(70)*4 - 10 )/3.; } //-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ // return (kScreen_width - WidthScale(70)*4 - 10 )/3.; //} -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{return UIEdgeInsetsMake(5, 5, 5, 5); }

關(guān)鍵點在這里:

1-===layout.scrolldirection = uicollectionViewScrollDirectionVertical;collection的滾動方向,由于要折行向下,所以方向是垂直方向,在排列的時候會默認選擇先將橫向排列完畢,折行向下。

2--=== minmumlinespaceing 這個這個屬性用來指示行與行之間的最小距離(縱向),或者列與列之間的最小距離(橫向)。無論橫向或者縱向,都可以滾動顯示所有內(nèi)容,所以這個屬性可以單獨設(shè)置。 ?SO 在不同的滾動方向上要分清楚他的作用,我就是在這里弄的頭暈?zāi)垦?#xff1b; ?當(dāng)然interitem就很好理解了 ?

?

另外還有一點是 ? 如果是自定義的layout,layout的代理方法是不會執(zhí)行的,若想達到相同的效果,需要在自定義的layout內(nèi)部進行處理!

? ? ? ? layout.scrollDirection = UICollectionViewScrollDirectionVertical;

?

轉(zhuǎn)載于:https://www.cnblogs.com/lidarui/p/6931415.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的关于collectionview布局的坑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。