我在同一视图控制器上有两个集合视图。为了区分它们,第一个标记为“ 1”,而另一个没有标记。直到今天,当我尝试添加样式时,它都可以正常工作。无论我尝试什么,只有第二个(未标记的)集合视图都将具有样式。
如何设置两个单元格的样式?两个单元格将具有完全相同的样式。
这是我最后的尝试:(单元格的标签/功能按预期工作,只是在集合视图上没有带有标签“ 1”的样式)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellThree", for: indexPath) as! CollectionViewCellThree
cell.firstLabel.text = self.firstArray[indexPath.item]
cell.secondLabel.text = self.secondArray[indexPath.item]
cell.thirdLabel.text = self.thirdArray[indexPath.item]
cell.setUpElementsThree(cell)
return cell
}
else{
let cellTwo = collectionView.dequeueReusableCell(withReuseIdentifier: "CellFour", for: indexPath) as! CollectionViewCellFour
cellTwo.firstLabelTwo.text = self.firstArrayTwo[indexPath.item]
cellTwo.secondLabelTwo.text = self.secondArrayTwo[indexPath.item]
cellTwo.thirdLabelTwo.text = self.thirdAmountArrayTwo[indexPath.item]
cellTwo.setUpElementsFour(cellTwo)
return cellTwo
}
}
然后在文件CollectionViewCellThree.swift和CollectionViewCellFour.swift下
//STYLING CODE BELOW
//outlets for styling
@IBOutlet weak var activeCell: UIView!
func setUpElementsThree(_ cell: UICollectionViewCell) {
//styling
//corners
cell.layer.cornerRadius = 6.0
//shadows
cell.layer.shadowColor = UIColor.black.cgColor
cell.layer.shadowOpacity = 0.2
cell.layer.shadowRadius = 5
cell.layer.shadowOffset = CGSize(width: 0, height: 5)
}
//STYLING CODE ABOVE
//STYLING CODE BELOW
//outlets for styling
@IBOutlet weak var usedCell: UIView!
func setUpElementsFour(_ cellTwo: UICollectionViewCell) {
//styling
//corners
cellTwo.layer.cornerRadius = 6.0
//shadows
cellTwo.layer.shadowColor = UIColor.black.cgColor
cellTwo.layer.shadowOpacity = 0.2
cellTwo.layer.shadowRadius = 5
cellTwo.layer.shadowOffset = CGSize(width: 0, height: 5)
}
//STYLING CODE ABOVE
我搜索了很长时间的错误,而答案一直都在我面前一直在调试器中。问题在于像元大小太大,无法加载样式。更改情节提要中的约束可解决此问题。这是在调试器中:
2020-05-11 16:22:37.361455-0400 APP[15727:3931440] The behavior of the UICollectionViewFlowLayout is not defined because:
2020-05-11 16:22:37.361746-0400 APP[15727:3931440] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
我学会了更深入地研究调试器。