【问题标题】:How to change UICollectionViewCell height?如何更改 UICollectionViewCell 高度?
【发布时间】:2016-04-20 18:23:08
【问题描述】:

我正在创建一个 Instagram 应用。所以,我有一个 UICollectionView,我在其中放置了我的图像、用户详细信息和喜欢和 cmets。

所以,问题是,我设置了单元格大小

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

并将初始大小设置为(宽度:320,高度:420(320 - 图片,50 - 图片顶部的用户详细信息和 50 - 图片下方的点赞数))。

但是取决于喜欢和 cmets 的数量,我的底部视图大小可能会改变,例如从 50 到 200。但在这种情况下,我的 UICollectionViewCell 不会改变它的大小。

如何使这取决于 cmets 视图高度我的 UICollectionViewCell 高度也会改变?

我正在研究它一个多星期,但不知道如何实现它。谁能帮帮我?

【问题讨论】:

  • 您是否正确设置了垂直约束?
  • 你需要实现一个方法来根据单元格的内容计算单元格的大小,并在sizeForItemAtIndexPath方法中使用这个方法
  • @zcui93 是的,我想
  • @MudOnTire 我有自定义单元格,所以它在另一个文件中(customCell.swift)

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout


【解决方案1】:
you need to get the height of string and then set cell size aacording to that string!!! 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
height=_collectionView.contentSize.height;
    width = _collectionView.frame.size.width;
    _collectionView.frame = CGRectMake(0,20,width, height);
}

#pragma MARK COLECTIONVIEW DELEGATE FLOW LAYOUT
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize Size =[[self.Array_LIKEs objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
    CGFloat calculate_width =Size.width+Size.width/2+20;
    CGFloat calculate_height=Size.height+10;
    return  CGSizeMake(calculate_width, calculate_height);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2014-09-21
    • 2020-12-19
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    相关资源
    最近更新 更多