UICollectionView - didDeselectItemAtIndexPath not called if cell is selected

后端 未结 7 1606
面向向阳花
面向向阳花 2020-12-01 09:12

The first thing I do is to set the cell selected.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndex         


        
7条回答
  •  清歌不尽
    2020-12-01 09:54

    I had the same issue. I pre-selected cells upon table load yet I had to double tap a cell to deselect it. @zeiteisen answer helped me. However, I'm working in Swift 3 so I thought I'd give an answer in Swift.

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
    
        if /*Test for Selection*/ {
            cell.isSelected = true
            collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .left)
        }        
        return cell
    }
    

提交回复
热议问题