【发布时间】:2015-07-30 00:22:58
【问题描述】:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
println(indexPath)
cell.imageView.contentMode = .ScaleAspectFill
cell.backgroundColor = UIColor.whiteColor()
cell.layer.borderColor = UIColor.blackColor().CGColor
cell.layer.borderWidth = 0.1
cell.frame.size.width = self.screenWidth / 3
cell.frame.size.height = self.screenWidth / 3
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as! String
cell.imageView.image = UIImage(contentsOfFile: self.getMediaFilePath(self.mediaModels[indexPath.row].pathToMedia))
cell.imageTicked.hidden = true
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 1
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
//cell.backgroundColor = UIColor.magentaColor()
self.mediaModels[indexPath.row].isSelected = true
cell.selected = true
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
//cell.backgroundColor = UIColor.whiteColor()
self.mediaModels[indexPath.row].isSelected = false
cell.selected = false
}
我可以在返回单元格的函数中隐藏 imageTicked,但我无法将其添加到 didSelectItemAtIndexPath 和 didDeselectItemAtIndexPath 中。
我想在选择项目时显示复选框,并在取消选择项目时隐藏复选框。
只是想知道如何在didDeselectItemAtIndexPath 内设置cell.imageTicked.hidden = true 和在didSelectItemAtIndexPath 内设置cell.imageTicked.hidden = false。
非常感谢您的帮助
【问题讨论】: