【问题标题】:Get UICollectionView height inside UIScrollView获取 UIScrollView 内的 UICollectionView 高度
【发布时间】:2017-08-08 14:47:38
【问题描述】:

我在滚动视图中有一个 UICollectionView,它的单元格具有动态宽度和静态高度。

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return tagArray.count;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    TagsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tagsCollectionViewCell" forIndexPath:indexPath ];
    [cell bindWithModel:tagArray[indexPath.row]];

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize yourLabelSize = [tagArray[indexPath.row] sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0f]}];

    return CGSizeMake(yourLabelSize.width+20+40, 40);

}

我可以使用以下方法计算高度:

CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height;

我得到的值高于实际高度。

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell


    【解决方案1】:
    collectionView:layout:sizeForItemAtIndexPath:
    

    向代理询问指定项目单元格的大小。

    CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height
    

    这将返回 collectionView 的高度,而不是 collectionView Cell 的高度。

    【讨论】:

    • 我没听明白。
    • CollectionView 高度与单个collectionviewcell 的高度不同。这里使用的委托方法是返回collectionviewcell的大小。
    • 我假设您将静态单元格高度 (40) 与 collectionViewContentSize.height 进行比较。
    • 不,我是使用 self.collectionView.collectionViewLayout.collectionViewContentSize.height 查找高度,而不是委托
    • 我需要collectionview的准确高度,因为它在滚动视图内。单元格的数量是动态的,单元格的宽度是动态的。
    猜你喜欢
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 2018-09-07
    • 2019-10-12
    • 2014-07-30
    • 1970-01-01
    相关资源
    最近更新 更多