选择集合中的项目视图出现时



>我正在尝试在集合视图中选择我的第一个单元格,当出现此处是它的代码时

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let indexPath = IndexPath(item: 0, section: 0)
categoryCollectionView.selectItem(at: indexPath
, animated: false, scrollPosition: UICollectionView.ScrollPosition(rawValue: 0))
self.collectionView(categoryCollectionView, didSelectItemAt: indexPath)
}

我的收藏ViewCell类中有一个对isSelected变量的覆盖来更改文本颜色

override var isSelected: Bool {
didSet {
if self.isSelected {
self.categoryName.textColor = UIColor(red: 237/255, green: 28/255, blue: 36/255, alpha: 1.0)
} else {
self.categoryName.textColor = UIColor(red: 63/255, green: 62/255, blue: 62/255, alpha: 1.0)
}
}
}

但它不起作用

我通过操作 isSelected 属性来解决它,它工作正常

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.categoryCollectionView {
if let cell = categoryCollectionView.dequeueReusableCell(withReuseIdentifier: "CategoryCollectionViewCell", for: indexPath) as? CategoryCollectionViewCell {
cell.configureCell(text: categories[indexPath.row].name)
if indexPath.row == 0 {
cell.isSelected = true
}
return cell
}
} 
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.categoryCollectionView {
if indexPath != IndexPath(row: 0, section: 0) {
collectionView.cellForItem(at: IndexPath(row: 0, section: 0))?.isSelected = false
}
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
categoryCollectionView.selectItem(at: indexPathForFirstRow, animated:false, scrollPosition: UICollectionView.ScrollPosition(rawValue: 0))
self.collectionView(paymentHeaderCollectionView, didSelectItemAt: indexPathForFirstRow)     
}

在DidSelect中,当用户选择另一个单元格时,您可以像这样取消选择单元格

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: IndexPath) {
let firstCell = IndexPath(item: 0, section: 0)
if indexPath != firstCell {
collectionView.deselectItem(at: indexPath, animated: false)
}
}

对于is选择部分,您可以尝试这个

override var selected: Bool {
get {
return super.selected
}
set {
if newValue {
super.selected = true
//your Color
println("selected")
} else if newValue == false {
super.selected = false
//your Color
println("deselected")
}
}
}

虽然这可能会导致问题,但在我看来,更简单和最好的解决方案是创建一个标志isSelected在您填充的对象中并检查它们并在didSelect中在它们之间切换

您可以像下面这样尝试,如果单元格在 0 索引中,它将对我有用。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ScenarioCollectionView.dequeueReusableCell(withReuseIdentifier: "ReuseScenarioCollectionViewCell", for: indexPath as IndexPath) as! ScenarioCollectionViewCell
if (indexPath.row == 0){
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
cell.layer.borderColor=UIColor.gray.cgColor        
}else{
cell.layer.borderColor=UIColor.white.cgColor
}
return cell
}

我希望它能为你工作。

相关内容

最新更新