0

I have done the following to assign an UIImage to different instance of UIView object. Inside my UIView, I have an UIImageView which holds the Image.

var transitionImageNames = ["1.png", "2.png", "3.png", "4.png"]

var count=0
for item in myList {
    var transitionImageName = transitionImageNames[count]
    item.transitionImageView.image = UIImage(named: transitionImageName)
// transitionImageName refers to 4 different images, let say 1,2,3,4.png
if (count<=3) {
    count += 1
}
}

there are a number of different items in myList. I have printed out the item instance, all of them have different instance value. However, I found that for the transitionImageView to be displayed, even if I set 1, 2, 3, 4.png to 4 different items in the myList, they all display the same image (which is the last one set)

Any idea on why?

user6539552
  • 1,331
  • 2
  • 19
  • 39
  • You code sets the same transitionImageName for all images. So it must be the same image shown. – ObjectAlchemist Apr 16 '17 at 13:18
  • share the code you have tried.. – Anil Kumar Apr 16 '17 at 13:18
  • Even your updated code do not show the problem. The problem is in fetching the transitionImageName. Please provide the code how you get it or add a print directly before using the var for own investigations. – ObjectAlchemist Apr 16 '17 at 13:30
  • Your newly updated code now does the same as in my answer below. If myList contains less or equal than four views it should do it's job. Before updating your code you wrote that you get transitionImageName by random. But now it's only a simple assignment. So I don't know how to help you any more. If you do not provide the necessary informations, there will be no better answer. – ObjectAlchemist Apr 16 '17 at 14:29

1 Answers1

3

I think you need something like this:

var transitionImageNames = ["1.png", "2.png", "3.png", "4.png"]
for (index, item) in myList.enumerated() {
   item.transitionImageView.image = UIImage(named: transitionImageNames[index])
}
ObjectAlchemist
  • 1,109
  • 1
  • 9
  • 18