标签周围的UICollectionView Wrap Cell

时间:2016-03-31 01:10:40

标签: ios swift uicollectionview uicollectionviewcell

我需要能够围绕Label包装UICollectionViewCell(带有一些填充)。基本上,我正在寻找类似这样的东西(这是在Android上完成的,没有太多麻烦): enter image description here

这是我目前在UICollectionView中的内容。每个单元格是一个设置宽度(屏幕宽度/ 3),但最后我希望每个单元格都是标签的宽度(+一些额外的填充):

enter image description here

我尝试覆盖sizeForItemAtIndexPath

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let cell = collectionView.cellForItemAtIndexPath(indexPath) //returns nil
    var width = cell?.bounds.width
    return CGSizeMake(width!, 50)

}

任何提示?谢谢!

1 个答案:

答案 0 :(得分:1)

sizeForItemAtIndexPath中返回文字的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return [(NSString*)[arrayOfStats objectAtIndex:indexPath.row] sizeWithAttributes:NULL];
}