如何预选和突出显示 UICollectionView 中的单元格

How to preselect and highlight a cell from a UICollectionView

我有一个数组,其中包含以前 selected 单元格的位置。 我想向用户显示以前 selected 的单元格(在另一个时刻,可能是几个月前),方法是突出显示它们并 selecting 它们(如果我之前单击一个 selected 我希望它取消select 它)当我的弹出窗口出现时,就像我在 :

中所做的那样
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
    } 
}
}

该单元格是蓝色的,所以它是 selected,但一旦完成我就无法取消select它。 发射后我 select 的细胞没有问题,我可以 select 和 deselect 它们没有问题。

对于您要执行的操作,请使用 UICollectionViewCell 中的 isSelected 属性。

关于isSelected的工作原理,请参考:https://medium.com/@p.gpt10/uicollectionviewcell-selection-made-easy-41dae148379d

对于最初选择 UICollectionViewCell 使用,

self.contentCollectionView.selectItem(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: .left)

此外,在didSelectItemAt方法中,您不需要更改isSelected或调用上述方法。只需参考上面的教程,你就会得到你所需要的一切。

编辑:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
    @IBOutlet weak var aCollectionView: UICollectionView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        collectionView.allowsMultipleSelection = true
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "aCell", for: indexPath as IndexPath) as! myCollectionViewCell
        cell.myLabel.text = "ok"
        cell.isSelected = false
        cell.layer.borderColor = UIColor.black.cgColor
        cell.layer.borderWidth = 2
        if indexPath.row == 5
        {
            collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .left) //Add this line
            cell.isSelected = true 
        } 
        return cell 
    } 

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning() 
    } 
}

class myCollectionViewCell: UICollectionViewCell
{
    @IBOutlet weak var myLabel: UILabel!

    override var isSelected: Bool{
        didSet{
            if self.isSelected
            {
                super.isSelected = true
                self.contentView.backgroundColor = UIColor(red:0.08, green:0.28, blue:0.45, alpha:1)
            }
            else
            {
                super.isSelected = false
                self.contentView.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7) 
            } 
        } 
    } 
}

我已经为你做了一些事情,希望对你有所帮助:

声明变量:

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()
}