Reputation: 309
I am setting the collection view cell size based on my string in it. I use this code and the result is ok for the first time, when reloading the collection view for the second time or more this happens(see the image please)
this is my code :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var i:Int = 0
var size = CGSize()
for t in mainCategoryDic {
if(i == indexPath.row) {
let index:String = t.key
size = (mainCategoryDic[index]?.name?.size(withAttributes: [NSAttributedString.Key.font : UIFont(name:font_name, size: 14)!]))!
}
i += 1
}
return CGSize(width: size.width + 38.0, height: 30.0)
}
Upvotes: 0
Views: 279
Reputation: 887
Try this:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var size = CGSize()
let t = mainCategoryDic[indexPath.item]
let index: String = t.key
size = (mainCategoryDic[index]?.name?.size(withAttributes: [NSAttributedString.Key.font : UIFont(name:font_name, size: 14)!]))!
return CGSize(width: size.width + 38.0, height: 30.0)
}
Upvotes: 1