【发布时间】:2014-12-13 00:45:19
【问题描述】:
好吧,假设我有 20 个单元格的 UICollection 在启用分页的情况下水平滚动并且可以在每页上容纳 9 个单元格,当我将其设为 10 个单元格而不是创建第二页时,它的移动刚好足以容纳第 10 个单元格这页纸。我希望它在滚动时在自己的页面上显示第 10 个单元格。
我也尝试过更改 collectionview.contentsize,但由于某种原因,无论我做什么,它都保持不变。
这是我的代码 -
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(82, 119);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(20, 10, 50, 10);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
customCell *cell= (customCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
return (UICollectionViewCell*)cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 20;
}
- (void)viewDidLoad {
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor lightGrayColor]];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
_collectionView.pagingEnabled = YES;
layout.minimumInteritemSpacing = 15;
layout.minimumLineSpacing = 25;
[_collectionView setContentInset:UIEdgeInsetsMake(65, 0, 0, 0)];
_collectionView.allowsMultipleSelection = YES;
[self.view addSubview:_collectionView];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
【问题讨论】:
-
这是默认流布局的工作方式。您可以增加单元格的大小、增加单元格之间的最小间距和/或增加 sectionInset。
标签: ios objective-c uicollectionview uicollectionviewlayout uicollectionviewdelegate