我在scrollview中有一个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;