Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStepan Ulyanin
11,318 PointsHow come the IBOutletCollection gives the index to the UIImageView that corresponds to our model (playlist)?
So we add the UIImageViews to the IBOutletCollection and they get some index assigned because the IBOutletCollection is an NSArray, what is the principle behind that? I remapped the UIImageView's in different order to the IBOutletCollection -> the icons changed too, so there is some connection between those (model and the view), I just can't connect it in my mind?
2 Answers
eirikvaa
18,015 PointsFrom a NSHipster post: The order of an outlet collection appears to be roughly the order in which their connections are established in Interface Builder. However, there are numerous reports of that order changing across versions of Xcode, or as a natural consequence of version control. Nonetheless, having code rely on a fixed order is strongly discouraged.
Seems you can't be guaranteed a fixed order with IBOutletCollection.
Tam Pham
3,203 PointsYou can do like this and it still works:
((UIImageView*)self.playlistImageViews[index]).image =playlist.playlistIcon; ((UIImageView*)self.playlistImageViews[index]).backgroundColor =playlist.backgroundColor;
The reason this statement: UIImageView *playlistImageView = self.playlistImageViews[i] works because playlistImageView is a pointer to an object in the array self.playlistImageViews. So when you do like playlistImageView.image = some image, you're actually doing self.playlistImageViews[i].image = some image.
Stepan Ulyanin
11,318 PointsStepan Ulyanin
11,318 PointsThanks for your answer but I think this is the part I can't understand: Pasan creates the IBOutletCollection of (UIImageView) and then in the implementation file he does this:
UIImageView *playlistImageView = self.playlistImageViews[i];
where playlistImageViews is the IBOutletCollection obviously, I can't understand the logic behind this assignment, intuitively I would do something like:
But I know that elements of NSArray are immutable:/ Sorry for all this questions, just want it to make sense to me
eirikvaa
18,015 Pointseirikvaa
18,015 PointsIt doesn't make any sense doing it the way you proposed, because I guess you would just overwrite the UIImageView object in the self.playlistImageViews array with the newly created playlistImageView object. You want to get and use the UIImageView object from the playlistImageViews array, so you assign it to the playlistImageView object.
Stepan Ulyanin
11,318 PointsStepan Ulyanin
11,318 Points// icon from playlist[0] -> playlistImageView.image that stores the UIImageView object from the IBOutletCollection with the index 0
That is where the binding happens; ok, it finally made sense, thank you for your patience