Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从UICollectionView中预选并高亮显示单元格_Ios_Swift_Swift3_Uicollectionviewcell - Fatal编程技术网

Ios 如何从UICollectionView中预选并高亮显示单元格

Ios 如何从UICollectionView中预选并高亮显示单元格,ios,swift,swift3,uicollectionviewcell,Ios,Swift,Swift3,Uicollectionviewcell,我有一个数组,其中我有以前选择的单元格的位置。 我希望向用户显示之前在其他时间选择的单元格,可以是几个月前,方法是突出显示这些单元格并选择它们。如果我单击以前选择的单元格,我希望它在弹出窗口出现时取消选择,如我在: func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 我尝试了不同的方法,比如: if (indexPath.row == 0 &&

我有一个数组,其中我有以前选择的单元格的位置。 我希望向用户显示之前在其他时间选择的单元格,可以是几个月前,方法是突出显示这些单元格并选择它们。如果我单击以前选择的单元格,我希望它在弹出窗口出现时取消选择,如我在:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
我尝试了不同的方法,比如:

if (indexPath.row == 0 && indexPath.section == 0){
        collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
} 
或:

但是什么都没用或者我看不见

你有什么帮助吗? 谢谢

编辑:

我现在做的是:

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
    } 
}
}
单元格是蓝色的,所以它已被选中,但一旦选中,我就无法取消选中它。
对于启动后我选择的单元格没有问题,我可以毫无问题地选择和取消选择它们。

对于您尝试执行的操作,请使用UICollectionViewCell的isSelected属性

有关isSelected的工作原理,请参阅:

对于最初选择UICollectionViewCell使用

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

编辑:


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

声明变量:

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

检查你的手机是否正常selectable@ReinierMelian我以前可以选择它,为什么现在不能呢?只是想通过编程实现。我也尝试过,比如:let indexPathForFirstRow=IndexPathitem:0,section:0 collectionView.cellForItemat:indexPathForFirstRow?.isSelected=true,但这不起作用。我第一次错过了一些东西,看起来它起作用了,我会在这方面做更多的工作,并让你知道。无论如何,谢谢。所以,看起来我可以通过编程方式选择我想要的单元格,但在。。。这很烦人,因为我真的需要它,你知道怎么做吗?当你选择另一个单元格时,该单元格将自动取消选择。
var selectIndex = 0
var selectIndexSec = 0
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()
}