gpt4 book ai didi

ios - UICollectionView 将单元​​格包裹在标签周围

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:32 25 4
gpt4 key购买 nike

我需要能够将 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)

}

有什么建议吗?谢谢!

最佳答案

sizeForItemAtIndexPath中返回文本的大小

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

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

关于ios - UICollectionView 将单元​​格包裹在标签周围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36322502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com