【发布时间】:2023-04-05 17:27:01
【问题描述】:
我有一个非常简单的演示,它使用 UICollectionView 显示图片列表,我使用自定义单元格类,其中包含如下 UIImage 和标签,没有约束。
然后我在代码中设置单元格的大小,如下所示。每行有 3 列。所有边距都设置为 10。
// Calculate size of cell
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let totalSpace = marginLeft + marginRight + (colomnSpacing * CGFloat(numberOfItemsPerRow - 1))
let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(numberOfItemsPerRow))
// 20 is for label height.
return CGSize(width: size, height: size + 20)
}
// Section margin, if you only have one section, then this is the CollectionView's margin.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: marginTop, left: marginLeft, bottom: marginBottom, right: marginRight) // top, left, bottom, right.
}
// Set row spacing
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return rowSpacing
}
// Set column spacing
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return colomnSpacing
}
这是用于设置图像和标签大小的代码。图像的宽度为屏幕宽度的 1/3(不包括列空间),高度等于宽度。标签宽度等于图片宽度,标签高度设置为20。
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SearchImageCell
// Set image size
cell.imageView.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height - 20)
cell.imageView.contentMode = .ScaleAspectFill
let currentItem = self.responseArray[indexPath.row]
let imageUrl = currentItem["imgurl"] as? String
let url = NSURL(string: imageUrl!)
// Load the image
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
let data = NSData(contentsOfURL: url!)
dispatch_async(dispatch_get_main_queue(), {
cell.imageView.image = UIImage(data: data!)
})
}
// Set label size
cell.imageDesc.frame = CGRect(x: 0, y: cell.imageView.frame.height, width: cell.frame.width, height: 20)
let shujia = currentItem["shujia"] as? String
cell.imageDesc.text = shujia!
cell.imageDesc.font = cell.imageDesc.font.fontWithSize(14)
cell.imageDesc.textAlignment = .Center
return cell
}
问题是:
当我第一次加载图像时,图像的大小不正确,如下所示,您可以看到图像的高度占据了整个单元格的高度,并且标签没有显示出来。
但是,当我向下和向上滚动时(这使得单元格被重复使用),图像被调整大小,我得到了正确的结果,这就是我想要的!
我尝试添加一些调试代码来打印单元格/图像/标签的大小,似乎所有大小都是正确的。
cell width: 100.0
cell height: 120.0
image width: 100.0
image height: 100.0
label width: 100.0
label height: 20.0
那么,怎么了?
在 SO(here,here) 上有一些类似的问题,但答案对我不起作用,比如
1. cell.layoutIfNeeded()
2. self.collectionViewLayout.invalidateLayout()
3. self.collectionView?.layoutIfNeeded()
请帮忙,如果您需要更多代码,请告诉我。
【问题讨论】:
-
您为什么不使用 AutoLayout 的任何原因?
-
使用
awakeFromNib设置imageView框架,而不是将其设置为cellForItemAtIndexPath。 -
您是否尝试在
cellFoItemAtIndexPath内将UIImageView和UILabel的translatesAutoresizingMaskIntoConstraints属性设置为false? -
@JoséRobertoAbreu
xib或storyboard将为他们设置 false -
@WilsonXJ 是的,你是对的。 link
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout