【问题标题】:How to preselect and highlight a cell from a UICollectionView如何从 UICollectionView 中预选和突出显示单元格
【发布时间】:2018-01-18 12:43:31
【问题描述】:

我有一个数组,其中包含先前选择的单元格的位置。 当我的弹出窗口出现,就像我在中所做的那样:

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

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

【问题讨论】:

  • 检查您的单元格是否可选择
  • @ReinierMelian 我以前可以选择它,为什么现在不能?只想以编程方式完成。

标签: ios swift swift3 uicollectionviewcell


【解决方案1】:

对于您要执行的操作,请使用 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) 
            } 
        } 
    } 
}

【讨论】:

  • 我也试过了,比如: let indexPathForFirstRow = IndexPath(item: 0, section: 0) collectionView.cellForItem(at: indexPathForFirstRow)?.isSelected = true 但这也不行跨度>
  • 编辑了答案。
  • 我第一次错过了一些东西,看起来它正在工作,我会在这方面做更多的工作并让你知道。还是谢谢。
  • 所以,看来我能够以编程方式选择我想要的单元格,但之后我无法取消选择它......这很烦人,因为我真的需要那个,你有一个知道怎么做吗?
  • 当您选择另一个单元格时,该单元格将被自动取消选择。
【解决方案2】:

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

声明变量:

var selectIndex = 0
var selectIndexSec = 0

在 viewDidload() 中:

collectionview.scrollToItem(at: IndexPath(item: selectIndex, section: selectIndexSec) , at: .centeredHorizontally, animated: false)

在collectionview委托中:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多