更改UICollecionViewCell之间的空间

时间:2018-06-08 04:28:58

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个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来设置连续列之间的最小间距。根据{{​​3}},我理解值表示水平滚动网格的连续列之间的最小间距。我也设置了我的细胞之间的空间。以下是this documentation的结果。

但要想看起来更好看。我想根据内容的大小更改单元格的宽度。所以我已经像这样更新了我的单元格框架。

- (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;
}

以下是更新单元格宽度后的结果。enter image description here

我不知道更改细胞大小后我的细胞之间的空间发生了变化的原因。有人可以就此提出建议吗?感谢。

2 个答案:

答案 0 :(得分:3)

不是将布局的itemSize属性设置为固定大小,而是实现collectionView:layout:sizeForItemAtIndexPath:布局委托方法。

然后您可以设置每个项目的正确大小。

答案 1 :(得分:2)

要实现这一目标,您需要在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);
}