我有一个集合视图,我重复使用它根据用户选择加载2个完全不同的数据集。对于每个数据集,集合视图具有不同的边框设置。
在初始加载时,集合视图单元格显示正确的边框,然后对于连续加载,先前的边框将保留在单元格中。
我在这个方法中设置了边框:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { }
我想要的是,在集合视图的每个负载上,单元格的边框也应该被重置。
编辑:以下是我构建单元格和设置边框的方法:
cell.label.text = MATRIX[indexPath.section][indexPath.row]
cell.layer.backgroundColor = UIColor.red.cgColor
if indexPath.section == 0 && indexPath.row == 0 {
cell.addBorders(left: YES, right: NO, top: YES, bottom: NO)
}
if indexPath.section == 0 && indexPath.row == 1 {
cell.addBorders(left: YES, right: YES, top: YES, bottom: YES)
}
if indexPath.section == 1 && indexPath.row == 0 {
cell.addBorders(left: YES, right: NO, top: NO, bottom: YES)
}
if indexPath.section == 1 && indexPath.row > 0 {
cell.addBorders(left: YES, right: NO, top: NO, bottom: YES)
}
if indexPath.section > 1 && indexPath.row == 1 {
cell.addBorders(left: YES, right: NO, top: NO, bottom: NO)
}
if indexPath.row == MATRIX[0].count {
cell.addBorders(left: NO, right: YES, top: NO, bottom: NO)
}
if indexPath.section == MATRIX.count {
cell.addBorders(left: NO, right: NO, top: NO, bottom: YES)
}
用于添加边框的UICollectionViewCell扩展方法:
func addBorders(left: Bool, right: Bool, top: Bool, bottom: Bool) {
if left {
self.layer.addBorder(edge: .left, thickness: 1)
}
if right {
self.layer.addBorder(edge: .right, thickness: 1)
}
if top {
self.layer.addBorder(edge: .top, thickness: 1)
}
if bottom {
self.layer.addBorder(edge: .bottom, thickness: 1)
}
}
CALayer扩展方法:
func addBorder(edge: UIRectEdge, thickness: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness)
break
case UIRectEdge.bottom:
border.frame = CGRect(x:0, y:self.frame.height - thickness, width:self.frame.width, height:thickness)
break
case UIRectEdge.left:
border.frame = CGRect(x:0, y:0, width: thickness, height: self.frame.height)
break
case UIRectEdge.right:
border.frame = CGRect(x:self.frame.width - thickness, y: 0, width: thickness, height:self.frame.height)
break
default:
break
}
border.backgroundColor = UIColor.darkGray.cgColor
self.addSublayer(border)
}
在集合视图单元格内部有一个名为prepareForReuse的实例方法:
override func prepareForReuse() {
super.prepareForReuse()
// make your borders nil or reset them
}
首先声明一个全局变量
var flagForBoarder = 1
然后检查集合视图cellforItemAt
if flagForBoarder == 1
{
cell.view.layer.boarderColor = UIcolor.red.cgcolor
}
else
{
cell.view.layer.boarderColor = UIcolor.blue.cgcolor
}
点击按钮或重新加载集合视图时设置FlagForBoarder值
flagForBoarder = 2
尝试使用两个自定义UICollectionViewCell
两个不同的数据集
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == gamePad{
var cell: UICollectionViewCell?
cell = gamePad.dequeueReusableCell(withReuseIdentifier: "coloredCell", for: indexPath)
cell?.backgroundColor = UIColor.gray
cell?.layer.cornerRadius = (cell?.frame.height)!/2
return cell!
}
if collectionView == blackCounterCV {
let cell = blackCounterCV.dequeueReusableCell(withReuseIdentifier: "blackC", for: indexPath) as! BlackCCollectionViewCell
cell.backgroundColor = UIColor.black
cell.layer.cornerRadius = (cell.frame.height)/2
cell.blackLbl.text = "TEST"
return cell
}
return UICollectionViewCell()
}