图像中带有勾选框的 UICollectionView 单元格
UICollectionView Cell with tick box in the image
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
。
非常感谢您的帮助
您只需创建一个 selectedIndexPath 变量。在 didSelectItemAtIndexPath 中设置,在 didDeselectItemAtIndexPath 中移除。记得调用 reloadData() 重新加载集合视图。
var selectedIndexPath: NSIndexPath?;
...
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath: NSIndexPath){
...
if selectedIndexPath != nil {
cell.imageTicked.hidden = selectedIndexPath.row == indexPath.row
}
else {
cell.imageTicked.hidden = true
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
...
self.selectedIndexPath = indexPath
collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
...
self.selectedIndexPath = nil
collectionView.reloadData()
}
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
。
非常感谢您的帮助
您只需创建一个 selectedIndexPath 变量。在 didSelectItemAtIndexPath 中设置,在 didDeselectItemAtIndexPath 中移除。记得调用 reloadData() 重新加载集合视图。
var selectedIndexPath: NSIndexPath?;
...
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath: NSIndexPath){
...
if selectedIndexPath != nil {
cell.imageTicked.hidden = selectedIndexPath.row == indexPath.row
}
else {
cell.imageTicked.hidden = true
}
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
...
self.selectedIndexPath = indexPath
collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath){
...
self.selectedIndexPath = nil
collectionView.reloadData()
}