简体   繁体   English

UICollectionView 单元格选择的奇怪行为

[英]Strange behavior with UICollectionView cell selection

I currently have this (pseudo)code:我目前有这个(伪)代码:

var selectedCell: UICollectionViewCell?

override func viewDidLoad() {
    super.viewDidLoad()
    ...
    #initialize all objects and pull data from the server to fill the cells

    UIView.animate(withDuration: 0, animations: {
        self.dataCollectionView.reloadData()
    }, completion: {(finished) in

        let indexPath = IndexPath(row: 0, section: 0)
        self.dataCollectionView.selectItem(at: indexPath, animated: true, scrollPosition: .top)
        self.collectionView(self.dataCollectionView, didSelectItemAt: indexPath)
    })
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! DataCollectionViewCell
    if !selectedCell {
        cell.layer.borderWidth = 1
    }
    else {
       cell.layer.borderWidth = 2            
    }
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAtindexPath: IndexPath) {

    let cell = collectionView.cellForItem(at: indexPath)
    selectedCell = cell
    cell.image = SomeImageFromServer
    cell.layer.borderWidth = 2
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    let cell = collectionView.cellForItem(at: indexPath)
    cell.layer.borderWidth = 1
}

My thinking is that this code will select the first cell right after the collection view has been loaded, and it does.我的想法是,这段代码将在集合视图加载后立即选择第一个单元格,并且确实如此。 The problem is it selects the last cell as well, but didSelectItemAtindexPath is never called for the last cell, and only the first cell.问题是它也选择了最后一个单元格,但是从不为最后一个单元格调用 didSelectItemAtindexPath,而只为第一个单元格调用。

I've tried selecting the second cell by using let indexPath = IndexPath(row: 1, section: 0) and it does select the second cell once the collectionview has been loaded, and the last cell is not selected as you would think.我尝试使用let indexPath = IndexPath(row: 1, section: 0)选择第二个单元格,并且一旦加载了集合let indexPath = IndexPath(row: 1, section: 0) ,它就会选择第二个单元格,并且没有像您想象的那样选择最后一个单元格。

And once any cell is selected, the last cell is unselected.一旦选择了任何单元格,最后一个单元格就不会被选中。

So my hypothesis is that this isn't the code thinking the cell is "selected" but that it's for some reason giving the selected cell a "selected cell border" but only when the first selected cell is the first one.所以我的假设是,这不是认为单元格被“选定”的代码,而是出于某种原因给选定的单元格一个“选定的单元格边框”,但仅当第一个选定的单元格是第一个时。 Any thoughts?有什么想法吗?

Try moving the border setting into cell, UICollectionView will automatically manage the border width:尝试将边框设置移动到单元格中,UICollectionView 会自动管理边框宽度:

//Swift3
class TestCollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        self.layer.borderWidth = 1 //Default border width
    }

    override var isSelected: Bool {
        didSet{
            if self.isSelected {
                self.layer.borderWidth = 2
            }
            else{
                self.layer.borderWidth = 1
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM