user1107173
user1107173

Reputation: 10784

Swift: Refreshing UIImageView Image

Half of my window is a UITableView and other is a PageViewController.

In my PageViewController, I have two properties:

@IBOutlet weak var displayImageView: UIImageView!
var displayImage : UIImage?

PageViewController viewDidLoad I assign:

   override func viewDidLoad() {
        super.viewDidLoad()
        self.displayImageView.image = self.displayImage
   }

displayImageView is an outlet connected to my scene in Storyboard. As the User taps different cells, I would like the image in displayImageView to change.

In my parentViewController that displays the tableView and the PageViewController, I call the following method in my didSelectRowAtIndexPath

func getDisplayImageForSelectedRow() {
    var row = self.tableView.indexPathForSelectedRow()?.row
    //DisplayImageVC is a contentViewController that is loaded by pageViewController
    var displayImageVC = self._pageContent[1] as DisplayImageVC
    displayImageVC.displayImage = self._displayImage[row!] as UIImage
    println(displayImageVC.displayImage)
    if (self.isPageViewVisible){
        displayImageVC.displayImageView.reloadInputViews()
        displayImageVC.displayImageView.setNeedsDisplay()
    }
}

Here the log from println():

You selected cell #0
Optional(<UIImage: 0x7c21c2e0>)
You selected cell #1
Optional(<UIImage: 0x7c21c5a0>)
You selected cell #2
Optional(<UIImage: 0x7c21cd00>)
You selected cell #3
Optional(<UIImage: 0x7c21cff0>)
You selected cell #4
Optional(<UIImage: 0x7c21d2d0>)

Based on the log, I presume a new image is being assigned; however, the UIImageView outlet continues to show the same image that was loaded the first time pageViewController was loaded.

As you may have noticed in the code, I have tried the following lines to code to get the UIImageView to refresh, but no avail:

displayImageVC.displayImageView.reloadInputViews()
displayImageVC.displayImageView.setNeedsDisplay()

How can I get the UIImageView to show the new image that was loaded?

Upvotes: 0

Views: 4351

Answers (1)

sergio
sergio

Reputation: 69047

In getDisplayImageForSelectedRow you are only updating the UIImage, you should also assign the image to the view.:

func getDisplayImageForSelectedRow() {
   ..
  displayImageVC.displayImage = self._displayImage[row!] as UIImage
  displayImageVC.displayImageView.image = displayImageVC.displayImage <--THIS
  ..
}

Upvotes: 1

Related Questions