我是一个阵列,我在此处选择了以前选定的单元格。 我想通过突出显示它们并选择它们来向用户显示先前选择的单元格(在另一个时刻,可能是几个月前)(如果我点击之前选择的单元格,我希望它取消选择当我的弹出窗口出现时,正如我所做的那样:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
我尝试过不同的事情:
if (indexPath.row == 0 && indexPath.section == 0){
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
}
或者:
let indexPathForFirstRow = IndexPath(item: 0, section: 0)
collectionView.cellForItem(at: indexPathForFirstRow)?.isSelected = true
但没有任何效果(或者我无法看到它......)
你有什么帮助可以带来这个吗? 谢谢!
编辑:
以下是我现在所做的事情:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
if (swipeDown![2][0] == (settings!.rowSelect) && indexPath.row == 4 && indexPath.section == 4) {
let indexPathspec = IndexPath(row: 4, section: 4)
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPathspec) as! MyCollectionViewCell
cell.isSelected = true
cell.myLabel.text = self.items[indexPath.item]
//cell.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 2
print("test")
return cell
}
cell.myLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 2
print(cell.isSelected)
print("11")
return cell
}
在MyCollectionViewCell中使用它
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
override var isSelected: Bool{
didSet{
//super.isSelected = true
self.contentView.backgroundColor = self.isSelected ? .blue : .green
}
}
}
细胞是蓝色的,所以它已被选中,但一旦完成,我就无法取消选择。 对于我在启动后选择的单元格没有问题,我可以毫无问题地选择和取消选择它们。
答案 0 :(得分:2)
对于您要执行的操作,请使用isSelected
的{{1}}属性。
如何选择有效,请参阅:https://medium.com/@p.gpt10/uicollectionviewcell-selection-made-easy-41dae148379d
最初选择UICollectionViewCell
使用
UICollectionViewCell
此外,在self.contentCollectionView.selectItem(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .left)
方法中,您无需更改didSelectItemAt
或调用上述方法。只需参考上面的教程,您将获得所需的一切。
修改强>
isSelected
答案 1 :(得分:0)
我已经为你做了一些事情,我希望它会对你有所帮助:
声明变量:
var selectIndex = 0
var selectIndexSec = 0
在viewDidload()中:
collectionview.scrollToItem(at: IndexPath(item: selectIndex, section: selectIndexSec) , at: .centeredHorizontally, animated: false)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell
if selectIndexSec == (indexPath as NSIndexPath).section
{
if selectIndex == (indexPath as NSIndexPath).row
{
cell.isSelected=true
}
else
{
cell.isSelected=false
}
}
else
{
cell.isSelected=false
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
selectIndex = (indexPath as NSIndexPath).row
selectIndexSec = (indexPath as NSIndexPath).section
collectionview.reloadData()
}