我想在选择时在我的集合视图中调整特定单元格的大小。我发现如何从以前的帖子中这样做,但遇到了问题。我在调试时发现选择落后于一,但我不知道为什么。
例如,如果我通过点击它选择一个单元格,则没有任何反应。如果我之后选择了另一个单元格,那么我选择的第一个单元格将被放大。如果我选择第三个单元格,则第二个单元格被放大。等等。
这就是我实现它的方式,只有一次单元格会像我想要的那样同时放大:
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,这与我上面描述的方式有关,但我不确定原因。
答案 0 :(得分:3)
问题在于您正在使用func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)
。
注意这句话:取消选择 ItemAtIndexPath。
选择 ItemAtIndexPath是一种可行的方式。