How to change an UIImageView into a custom cell

不问归期 提交于 2019-12-08 09:12:04

问题


I have an UIImageView in a custom cell (@IBOutlet var imageSquadra: UIImageView!) and in the TableViewController I initialize this imageView:

let squadra = DataManager.sharedInstance.arrayCori[indexPath.row]

        cell.imageSquadra.image = squadra.sfondo1

Now I need to change the image touching up inside the cell with an other image, and return to the previous image touching up into an other cell. Is it hard to do?

EDIT: or if was easier, would be fine apply a CAFilter like a shadow to the image.


回答1:


On your tableview set multiple selection to NO:

tableView.allowsMultipleSelection = false

Then you want something like this on your TableViewController

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

   // Store which indexPath has been selected in a global variable

   tableView.reloadData()
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.reloadData()
}

In your cellForRow method you want something like...

cell.useSelectedImage(indexPath == storedIndexPath)

And finally in your custom cell class you want to add a method that does similar to this

func useSelectedImage(bool:useSelected)
{
     self.myImage = useSelected ? "selectedImage" : "unselectedImage"
}

My Swift is a bit rusty... but you should be able to get the general idea from what I've done here



来源:https://stackoverflow.com/questions/32376856/how-to-change-an-uiimageview-into-a-custom-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!