如何解决将视图添加到自定义集合视图单元格时的奇怪功能
将内容添加到客户视图单元时,我得到了意外的功能。
这是代码,然后我将解释所发现的内容。
控制器代码:
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()
}
}
}
基本上,使用现在的这段代码,即使我将它们添加到内容视图中,由于某种原因,也没有任何显示在单元格中。但是,我可以在视图层次结构的调试器中看到生成的单元格和正确的大小:
现在,当我取消注释自动布局约束的代码行时,图像和标签现在出现,但是单元格缩小以匹配这些视图:
使用集合视图和单元格,我还是swift / iOS的新手,并试图了解为什么会这样。根据我的理解,自动布局约束是在单元格内容视图内部的图像视图上设置的,因此它们不应该增长以匹配单元格内容视图而不是缩小单元格内容视图吗?另外,为什么在添加约束之前,视图(图像和标签)现在显示在单元格内容视图中?我不清楚。如果有人知道解决方案,我可能会想念它,为什么会发生这种情况,将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。