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
中添加它。
我想在选择项目时显示复选框,并在取消选择项目时隐藏复选框。
只是想知道如何在cell.imageTicked.hidden = true
内didDeselectItemAtIndexPath
和cell.imageTicked.hidden = false
内didSelectItemAtIndexPath
设置import org.json4s._
import org.json4s.jackson.JsonMethods._
import scala.io.Source
case class Record(key1:String, key2:String, key3:String)
implicit val format = DefaultFormats
val records = Source.fromFile("JsonFile.json").getLines.map(parse(_).extract[Record]).toList
\\ records will be a List[Record], with elements accessible like
records(1).key2
。
非常感谢您的帮助
答案 0 :(得分:1)
您只需要创建一个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()
}