更改 UICollecionViewCell 之间的 space
Change the space between UICollecionViewCell
我有一个 CollectionView。这是我的 CollectionView 的实现。
- (void)setupCollectionView{
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
[self.flowLayout setSectionInset:UIEdgeInsetsMake(0, 20, 0, 0)];
self.flowLayout.itemSize = CGSizeMake(150, 35);
self.flowLayout.minimumLineSpacing = 20;
[self.flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.vwCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.vwQuickReplyContainer.frame.size.width, self.vwQuickReplyContainer.frame.size.height) collectionViewLayout:self.flowLayout];
self.vwCollectionView.backgroundColor = [UIColor clearColor];
[self.vwCollectionView registerNib:[UINib nibWithNibName:@"QuickrepliesCell" bundle:nil] forCellWithReuseIdentifier:@"QuickrepliesCell"];
[self.vwQuickReplyContainer addSubview:self.vwCollectionView];
self.vwQuickReplyContainer.hidden = YES;
[self.vwCollectionView setShowsHorizontalScrollIndicator:NO];
[self.vwCollectionView setDataSource:self];
[self.vwCollectionView setDelegate:self];
[self.vwCollectionView reloadData];
}
这里我设置了minimumLineSpacing来设置连续列之间的最小间距。根据this documentation, I understand that value represents the minimum spacing between successive columns for a horizontally scrolling grid. I was also set the space between my cells. Here is the outcomes of doing that .
但是为了好看。我想根据内容的大小更改单元格的宽度。所以我已经像这样更新了我的单元格框架。
- (QuickrepliesCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuickrepliesCell" forIndexPath:indexPath];
QuickReply *model = [[QuickReply alloc]initWithDictionary:self.quickRepliesArr[indexPath.row] error:nil];
self.cell.lblOpion.text = model.title;
CGFloat width = [self.cell.lblOpion.text sizeWithFont:WWLATOREGULAR(15)].width;
self.cell.frame = CGRectMake(self.cell.frame.origin.x, self.cell.frame.origin.y, width + 24, self.cell.frame.size.height);
return self.cell;
}
这是更新单元格宽度后的结果。。
我不知道更改单元格大小后单元格之间的 space 发生变化的原因。任何人都可以对此提出建议吗?谢谢。
要实现它,您需要在 collectionView:layout:sizeForItemAtIndexPath:
中计算和更改项目大小
- (QuickrepliesCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuickrepliesCell" forIndexPath:indexPath];
QuickReply *model = [[QuickReply alloc]initWithDictionary:self.quickRepliesArr[indexPath.row] error:nil];
self.cell.lblOpion.text = model.title;
return self.cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
QuickReply *model = [[QuickReply alloc]initWithDictionary:self.quickRepliesArr[indexPath.row] error:nil];
CGFloat width = [model.title sizeWithFont:WWLATOREGULAR(15)].width;
return CGSizeMake(width + 24, 35);
}
不要将布局的 itemSize
属性 设置为固定大小,而是实施 collectionView:layout:sizeForItemAtIndexPath:
布局委托方法。
然后您可以设置每个项目的适当大小。
我有一个 CollectionView。这是我的 CollectionView 的实现。
- (void)setupCollectionView{
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
[self.flowLayout setSectionInset:UIEdgeInsetsMake(0, 20, 0, 0)];
self.flowLayout.itemSize = CGSizeMake(150, 35);
self.flowLayout.minimumLineSpacing = 20;
[self.flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
self.vwCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.vwQuickReplyContainer.frame.size.width, self.vwQuickReplyContainer.frame.size.height) collectionViewLayout:self.flowLayout];
self.vwCollectionView.backgroundColor = [UIColor clearColor];
[self.vwCollectionView registerNib:[UINib nibWithNibName:@"QuickrepliesCell" bundle:nil] forCellWithReuseIdentifier:@"QuickrepliesCell"];
[self.vwQuickReplyContainer addSubview:self.vwCollectionView];
self.vwQuickReplyContainer.hidden = YES;
[self.vwCollectionView setShowsHorizontalScrollIndicator:NO];
[self.vwCollectionView setDataSource:self];
[self.vwCollectionView setDelegate:self];
[self.vwCollectionView reloadData];
}
这里我设置了minimumLineSpacing来设置连续列之间的最小间距。根据this documentation, I understand that value represents the minimum spacing between successive columns for a horizontally scrolling grid. I was also set the space between my cells. Here is the outcomes of doing that
但是为了好看。我想根据内容的大小更改单元格的宽度。所以我已经像这样更新了我的单元格框架。
- (QuickrepliesCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuickrepliesCell" forIndexPath:indexPath];
QuickReply *model = [[QuickReply alloc]initWithDictionary:self.quickRepliesArr[indexPath.row] error:nil];
self.cell.lblOpion.text = model.title;
CGFloat width = [self.cell.lblOpion.text sizeWithFont:WWLATOREGULAR(15)].width;
self.cell.frame = CGRectMake(self.cell.frame.origin.x, self.cell.frame.origin.y, width + 24, self.cell.frame.size.height);
return self.cell;
}
这是更新单元格宽度后的结果。
我不知道更改单元格大小后单元格之间的 space 发生变化的原因。任何人都可以对此提出建议吗?谢谢。
要实现它,您需要在 collectionView:layout:sizeForItemAtIndexPath:
- (QuickrepliesCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuickrepliesCell" forIndexPath:indexPath];
QuickReply *model = [[QuickReply alloc]initWithDictionary:self.quickRepliesArr[indexPath.row] error:nil];
self.cell.lblOpion.text = model.title;
return self.cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
QuickReply *model = [[QuickReply alloc]initWithDictionary:self.quickRepliesArr[indexPath.row] error:nil];
CGFloat width = [model.title sizeWithFont:WWLATOREGULAR(15)].width;
return CGSizeMake(width + 24, 35);
}
不要将布局的 itemSize
属性 设置为固定大小,而是实施 collectionView:layout:sizeForItemAtIndexPath:
布局委托方法。
然后您可以设置每个项目的适当大小。