Ios 将视图添加到自定义集合视图单元格时的奇怪功能
在向客户视图单元格添加内容时,我得到了意想不到的功能 这是代码,然后我会解释我发现了什么 控制器代码:Ios 将视图添加到自定义集合视图单元格时的奇怪功能,ios,swift,uicollectionview,autolayout,Ios,Swift,Uicollectionview,Autolayout,在向客户视图单元格添加内容时,我得到了意想不到的功能 这是代码,然后我会解释我发现了什么 控制器代码: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let bedCell = collectionView.dequeueReusableCell(withReuseIdentifier
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let bedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "BedCell", for: indexPath) as! BedCollectionViewCell
bedCell.cellsBedModel = cottageModel!.bedsList[indexPath.item]
bedCell.setupBedCell()
return bedCell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let columns: CGFloat = 2
var width: CGFloat
var height: CGFloat
let widthOfCollection = collectionView.bounds.width
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let spaceBetweenCells = flowLayout.minimumInteritemSpacing * (columns - 1)
let adjustedWidth = widthOfCollection - spaceBetweenCells
width = floor(adjustedWidth / columns)
height = width
return CGSize(width: width, height: height)
}
自定义单元格代码:
import UIKit
class BedCollectionViewCell: UICollectionViewCell {
var cellsBedModel: Bed?
//view objects to place in the cell
var bedImageView = UIImageView()
var bedSizeLabel = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
layer.cornerRadius = 10
layer.shadowOpacity = 1
layer.shadowOffset = CGSize(width: 1, height: 1)
}
func setupBedCell() {
//create the cell's bed image and place it in the image view
let bedImage = UIImage(systemName: "bed.double")
bedImageView.image = bedImage
bedImageView.contentMode = .scaleAspectFit
contentView.backgroundColor = .clear
bedSizeLabel.text = String(describing: cellsBedModel!.size)
bedSizeLabel.textAlignment = .center
bedSizeLabel.textColor = .black
bedSizeLabel.backgroundColor = .gray
//add the view objects to the cell's content view
//set the cell's content constraints
setImageConstraints()
setLabelConstraints()
}
func setImageConstraints() {
self.contentView.addSubview(bedImageView)
//bedImageView.translatesAutoresizingMaskIntoConstraints = false
//bedImageView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
//bedImageView.bottomAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true
//bedImageView.widthAnchor.constraint(equalTo: self.contentView.widthAnchor).isActive = true
}
func setLabelConstraints() {
self.contentView.addSubview(bedSizeLabel)
//bedSizeLabel.translatesAutoresizingMaskIntoConstraints = false
//bedSizeLabel.topAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true
//bedSizeLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
//bedSizeLabel.widthAnchor.constraint(equalTo: self.contentView.widthAnchor).isActive = true
}
override func prepareForReuse() {
super.prepareForReuse()
//removes all children views from the cell before reusing a cell
for cell in self.contentView.subviews {
cell.removeFromSuperview()
}
}
}
基本上,现在使用这段代码,由于某种原因,单元格中不会显示任何内容,即使我将它们添加到内容视图中。但是,我可以在视图层次结构的调试器中看到,生成的单元格大小正确:
现在,当我取消注释autolayout约束的代码行时,图像和标签现在会出现,但单元格会缩小以匹配这些视图:
对于集合视图和单元格,我还是swift/iOS新手,并试图理解为什么会发生这种情况。根据我的理解,自动布局约束是在单元格内容视图内部的图像视图上设置的,因此它们不应该增长以匹配单元格内容视图,而不是收缩单元格内容视图吗?另外,在添加约束之前,为什么视图(图像和标签)现在显示在“单元格内容”视图中?我对此不清楚。如果有人知道我可能缺少的解决方案以及为什么会发生这种情况,我们将不胜感激 你能创建一个小项目,在那里你可以复制这个问题并分享它吗?快速调试问题将非常好。请尝试在集合的故事板中将
估计大小设置为None
view@aheze就是这样,作为iOS的新手,很难跟踪所有这些事情。非常感谢!!Np,我刚才遇到了这个问题。你能创建一个小项目,在那里你可以重现这个问题并分享它吗?快速调试问题将非常好。请尝试在集合的故事板中将估计大小设置为None
view@aheze就是这样,作为iOS的新手,很难跟踪所有这些事情。非常感谢!!Np,我刚才也有这个问题