繁体   English   中英

标签周围的UICollectionView Wrap Cell

[英]UICollectionView Wrap Cell around Label

我需要能够围绕Label包装UICollectionViewCell(带有一些填充)。 基本上,我正在寻找这样的东西(这是在Android上完成的,没有太多麻烦): 在此输入图像描述

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

在此输入图像描述

我试过覆盖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)

}

有小费吗? 谢谢!

sizeForItemAtIndexPath返回文本的大小

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM