【发布时间】:2021-04-05 22:20:10
【问题描述】:
我在 Swift 中使用 UICollectionView,遇到了一个我不知道它来自哪里的奇怪错误。
所以我有一个正常的收藏视图设置:
func setupCollectionView() {
ColorTagViewCell.registerCellByNib(self.colorTagCollection)
self.colorTagCollection.delegate = self
self.colorTagCollection.dataSource = self
}
函数委托的实现:
extension EventViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 20, height: 20)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return colorStringList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = ColorTagViewCell.loadCell(self.colorTagCollection, path: indexPath) as? ColorTagViewCell else {return UICollectionViewCell()}
cell.setupViewForCell(colorString: colorStringList[indexPath.row], state: colorSelection[indexPath.row])
return cell
}
}
当我运行应用程序时,虽然我为每个单元格设置了默认背景颜色,但集合视图单元格没有出现
我还没有实现任何单元格交互方法或单击或类似的东西。我不知道这个错误是什么,我已经尝试修复它一天但没有任何运气。 我还在 viewDidLoad() 中调用 reloadData() 并调试 UI,并注意到我的集合视图仍然存在,只是没有显示颜色。
我的 ColorTagViewCell 类,我在其中格式化和设置单元格:
class ColorTagViewCell: BaseCLCell {
@IBOutlet weak var outerView: UIView!
@IBOutlet weak var innerView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
self.roundedView(views: [self.outerView, self.innerView])
}
func roundedView(views: [UIView]) {
views.forEach { (view) in
view.layer.cornerRadius = view.frame.size.width / 2
view.layer.masksToBounds = true
}
}
func setupViewForCell(colorString: String, state: Bool) {
self.outerView.backgroundColor = UIColor.colorFromHexString(hex: colorString)
self.innerView.isHidden = state
}
}
【问题讨论】:
-
你能说明你在哪里设置这些单元格的背景颜色,以及你通常在哪里设置它们的格式吗?也许他们在那里但看不见。
-
ibb.co/Zzx7PhV,这是我格式化和设置单元格的代码,不要介意 innerView 因为它是隐藏的,我之前评论过那行代码但它没有帮助,因为您可以看到我为它们中的每一个设置了背景颜色,但它只是在我运行应用程序时不出现,它只是在我单击它后出现。 ibb.co/xqY7QNc,当我捕获 UI 时的另一张图片,单元格确实有背景颜色,正如您在右侧面板中看到的那样,当它们没有出现时,这真的很奇怪,但它们仍然在视图中
-
请edit您的问题以文本形式包含相关代码
-
我刚刚编辑了我的问题以包含我的 CollectionViewCell 类的代码,我在其中格式化和设置单元格。还有什么我应该补充的来澄清我的问题吗?
-
你能说明你在哪里设置使用的数组吗?另外,你在哪里调用 setupCollectionView()
标签: ios swift xcode uicollectionview