Ios UICollectionView单元布局未重置以前设置的边框
我有一个集合视图,我可以根据用户的选择重用它来加载2个完全不同的数据集。对于每个数据集,集合视图都有不同的边框设置 初始加载时,“集合视图”单元显示正确的边框,然后对于连续加载,先前的边框将保留在单元中 我在此方法内设置了边框:Ios UICollectionView单元布局未重置以前设置的边框,ios,swift,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我有一个集合视图,我可以根据用户的选择重用它来加载2个完全不同的数据集。对于每个数据集,集合视图都有不同的边框设置 初始加载时,“集合视图”单元显示正确的边框,然后对于连续加载,先前的边框将保留在单元中 我在此方法内设置了边框: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { } 我想要的是,在每次加载
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)
}
在集合视图单元格中有一个名为的实例方法:
override func prepareForReuse() {
super.prepareForReuse()
// make your borders nil or reset them
}
在集合视图单元格中有一个名为的实例方法:
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
}
点击按钮或重新加载collectionview时,设置FlagForBoarder值
flagForBoarder = 2
首先声明一个全局变量,如
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
尝试对两个不同的数据集使用两个自定义的
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()
}
尝试对两个不同的数据集使用两个自定义的
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()
}
这可能是一个很好的答案,但请解释一下他如何使用这种方法来实现他的目标。当我在开发的起跑线上时,我在论坛和个人资料组上问了很多问题,“我如何实现这个和那个”以及“我的代码有什么问题,为什么不起作用”。我很高兴,很多人没有给我一个拷贝粘贴解决方案,但给了我一个点,我必须自己研究它。在我的例子中,根据@vasu的描述,我做出了一个决定,那就是重用单元的问题。我给了一个研究的起点,下一步不取决于me@PeteStreem感谢您提供此指针,但不幸的是,它无法完美工作。即使没有添加任何边框,我也会随机看到边框。由于声誉不好,我无法上传示例图像:(@Vasu您可以将代码上传到github,我们可以查看;)或者用更多的代码行更新您的问题,以便我们可以查看具体内容wrong@PeteStreem当然但我注意到的是,即使我没有为任何单元格提供任何边框,即使我没有重新加载集合视图,一些单元格也会有一些边框。当背景是白色时,它被忽略了。当我设置背景色时,边距是可见的。这可能是一个很好的答案,但请解释一下他如何使用这种方法来实现他的目标。当我处于开发的起点时,我在论坛和个人资料组上问了很多问题,“我如何实现这个和那个”以及“我的代码有什么问题,为什么它不起作用”。我很高兴,很多人没有给我一个拷贝粘贴解决方案,但给了我一个点,我必须自己研究它。在我的例子中,根据@vasu的描述,我做出了一个决定,那就是重用单元的问题。我给了一个研究的起点,下一步不取决于me@PeteStreem感谢您提供此指针,但不幸的是,它无法完美工作。即使没有添加任何边框,我也会随机看到边框。由于声誉不好,我无法上传示例图像:(@Vasu您可以将代码上传到github,我们可以查看;)或者用更多的代码行更新您的问题,以便我们可以查看具体内容wrong@PeteStreem当然但我注意到的是,即使我没有为任何单元格提供任何边框,即使我没有重新加载集合视图,一些单元格也会有一些边框。当背景是白色时,它被忽略了。当我设置背景色时,边距是可见的。您提供的代码太短。请提供更多的代码,以便更容易找到您的实际问题!发布完整的“cellForItemAt”方法。您必须缺少其他条件。您提供的代码太短。请提供更多的代码,以便更容易找到您的实际问题!发布完整的“cellForItemAt”方法。您必须缺少其他条件无关,但变量名称在Swift中应使用小写。请告诉我,您所指的单元格视图是什么?@Vasu该视图位于cellForItemAt中,但变量名称在Swift中应使用小写。请告诉我,你指的是什么cell.view?@Vasu该视图在cell内这与我的代码类似,只是在图层上添加了边框,而不是corder radius。但它在连续加载时添加了两个边框。这与我的代码类似,只是在图层中添加了边框,而不是corder radius。但它会在连续加载时添加两个边界。