首次加载时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
:
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屋!