首次加载时 UICollectionViewCell 内容大小错误 [英] UICollectionViewCell content wrong size on first load
问题描述
我在 UIViewController
中添加了一个 UICollectionView
并为它制作了一个自定义单元格.
I have a added a UICollectionView
to my UIViewController
and have made a custom cell for it.
我使用 sizeForItemAtIndexPath
方法设置单元格的大小,并将每个单元格的高度和宽度设置为 UIScreen.mainScreen().bounds.width/2
.所以本质上,每个单元格的宽度是屏幕的一半,高度也是相同的长度.
I set the size of the cell using the sizeForItemAtIndexPath
method and set each cell to be UIScreen.mainScreen().bounds.width/2
in both height and width. So essentially, each cell is half the screen wide and this same length in height.
然后我在单元格内有一个 UIImageView
,每个边缘都固定到单元格的相对边缘,以便图像视图填充单元格.我还有一个 UILabel
在单元格中垂直和水平居中.
I then have an UIImageView
inside the cell with each edge pinned to its relative edge of the cell so that the image view fills the cell. I also have a UILabel
that is centred both vertically and horizontally in the cell.
但是在第一次加载时,单元格的大小是正确的,但左上角的一个小方块中的内容要小得多.(如下图所示,我已将单元格的 contentView 背景颜色设置为绿色,imageView 背景颜色设置为红色).
However on first load, the cell is the correct size but the content is much smaller in a tiny square in the top left. (Seen in the image below, I have set the cell's contentView background color to green and the imageView background color to red).
然后在向下滚动一点之后(我假设一个可重复使用的单元格正在出队)所有单元格都很好(并且在向上滚动后仍然很好).
Then after scrolling down a little bit (and I assume a reusable cell being dequeued) all of the cells are fine (and remain fine after scrolling back up).
是什么导致内容在首次加载时没有被正确限制?
What is causing the content to not be constrained properly on first load?
更新
我的 UIViewController
中的一些代码:
Some of the code from my UIViewController
:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("PrevDrawCell", forIndexPath: indexPath) as? PrevDrawCell
if cell == nil {
cell = PrevDrawCell()
}
cell!.drawVC = self
cell!.imageShortCode = self.tempImageURLs[indexPath.row]
// Pull image (async) and set after download
cell!.setupImage()
cell!.contentView.backgroundColor = UIColor.greenColor()
cell!.coverView.backgroundColor = UIColor.redColor()
cell!.dateLabel.font = UIFont(name: "HelveticaNeue-Light", size: 26)
cell!.dateLabel.text = "(self.tempImageURLs.count-indexPath.row)/9"
return cell!
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = UIScreen.mainScreen().bounds.width/2
return CGSize(width: size, height: size)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsZero
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0
}
推荐答案
看起来它仍然保留了 contentView 的 100x100px 基本大小,而单元格本身已经获得了正确的大小.您可以在返回单元格之前强制重置布局:
It looks like it still retains it's 100x100px basic size for the contentView while the cell itself gets the right size already. You can force the layout to reset adding the following line just before you return the cell:
cell?.layoutIfNeeded()
这篇关于首次加载时 UICollectionViewCell 内容大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!