我想在选择 Collection View 中的特定单元格时调整其大小。我从之前的帖子中找到了如何做到这一点,但遇到了问题。我在调试时发现选择落后了一位,但我不确定为什么。
例如,如果我通过点击选择一个单元格,则不会发生任何情况。如果我之后选择另一个单元格,那么我选择的第一个单元格就会放大。如果我选择第三个单元格,则第二个单元格会放大。等等。
这就是我实现它的方式,只有当单元格像我想要的那样同时放大时:
var selectedIndexPath: NSIndexPath!
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if selectedIndexPath != nil { //We know that we have to enlarge at least one cell
if indexPath == selectedIndexPath {
return CGSize(width: self.gallery.frame.size.width, height: self.gallery.frame.size.width)
}
else {
return CGSize(width: self.gallery.frame.size.width/3.0, height: self.gallery.frame.size.width/3.0)
}
}
else {
return CGSize(width: self.gallery.frame.size.width/3.0, height: self.gallery.frame.size.width/3.0)
}
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
if selectedIndexPath != nil && selectedIndexPath == indexPath
{
selectedIndexPath = nil //Trigger large cell set back to normal
}
else {
selectedIndexPath = indexPath //User selected cell at this index
}
collectionView.reloadData()
}
我在调试时发现选择在 didDeselectItemAtIndexPath 中以我上面描述的方式滞后,但不确定为什么。
最佳答案
问题是您正在使用func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
。
注意这句话:didDeselectItemAtIndexPath。
didSelectItemAtIndexPath 是一种可行的方法。
关于ios - UICollectionView 放大选择的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34478329/