我在自定义UICollectionViewCell
中显示4张图像作为黑白图像
图像突出显示,我想显示另一个图像但是颜色正如我在didHighlightItemAtIndexPath
中尝试的那样。
我怎么能这样做,非常感谢任何建议。
如果可能,当用户释放触摸单元格时,图像会变回黑白:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! menuCollectionViewCell
// Display the country name
if let value = countries[indexPath.row]["nameEnglish"] as? String {
cell.cellTitle.text = value
}
// Display "initial" flag image
let initialThumbnail = UIImage(named: "question")
cell.cellImage.image = initialThumbnail
// Fetch final flag image - if it exists
if let value = countries[indexPath.row]["blackWhite"] as? PFFile {
let finalImage = countries[indexPath.row]["blackWhite"] as? PFFile
finalImage!.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
cell.cellImage.image = UIImage(data:imageData)
}
}
}
}
return cell
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! menuCollectionViewCell
if cell.cellImage.highlighted == true {
if let value = countries[indexPath.row]["flag"] as? PFFile {
let finalImage = countries[indexPath.row]["flag"] as? PFFile
finalImage!.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
cell.cellImage.image = UIImage(data:imageData)
}
}
}}
// return cell
}
}
或者我应该改变它:
// Process collectionView cell selection
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! menuCollectionViewCell
if indexPath.row == 0 {
let currentObject = countries[indexPath.row]
self.performSegueWithIdentifier("CollectionViewToTableView", sender: currentObject)
}
collectionViewCell
class menuCollectionViewCell: UICollectionViewCell {
@IBOutlet var cellTitle: UILabel!
@IBOutlet var cellImage: UIImageView!
func toggleSelected ()
{
if (selected){
backgroundColor = UIColor.orangeColor()
}else {
backgroundColor = UIColor.whiteColor()
}
}
}