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

标签 ios swift swift3 uicollectionviewcell

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

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

单元格是蓝色的,所以它被选中了,但是一旦完成我就不能取消它。 启动后我选择的单元格没有问题,我可以毫无问题地选择和取消选择它们。

最佳答案

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

关于 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) 
            } 
        } 
    } 
}

关于ios - 如何预选和突出显示 UICollectionView 中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45610785/

相关文章:

ios - iAd如何根据国家选择广告?

ios - 应用程序启动期间 UITabBarController 顶部的 ModalView

ios - 在 Swift 中创建 block 不起作用

ios - UICollectionView 不显示

IOS AVFoundation - 方形视频

ios - 使用文档选择器从 iCloud 中选择一个文件夹

swift - 将动画角色从 mixamo 导入到 Reality Composer 时出现问题(从 .FBX 转换为 USDZ)

swift - RealmSwift 中的 RLMCollection 替换是什么

swift - 核心数据 - 改变属性类型

ios - 桥接 header 中导入的 header 在 'Release' 构建配置中不可见,但在 'Debug' 构建配置中可见