我有一个收集视图,我可以根据用户的选择重用它来加载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)
}
答案 0 :(得分:0)
在集合视图单元格中有一个 instance方法,称为 prepareForReuse :
override func prepareForReuse() {
super.prepareForReuse()
// make your borders nil or reset them
}
答案 1 :(得分:0)
首先声明一个全局变量,例如
var flagForBoarder = 1
然后您在集合视图中检入cellforItemAt
if flagForBoarder == 1
{
cell.view.layer.boarderColor = UIcolor.red.cgcolor
}
else
{
cell.view.layer.boarderColor = UIcolor.blue.cgcolor
}
在点击按钮或重新加载collectionview时设置FlagForBoarder值
flagForBoarder = 2
答案 2 :(得分:0)
尝试对两个不同的数据集使用两个自定义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()
}