开发者

IOS: fill a NSArray with UIImageView

In my project I created 30 small UIImageView where inside I put a different background. This is my group of UIImageView without background

IOS: fill a NSArray with UIImageView

and this is the group of UIImageView with different background:

IOS: fill a NSArray with UIImageView

I put these group of UImageView inside a UITableViewCell and I use them to define a timeline, but it's not important. I declared in .h these UIImageView but now I must insert theme in an NSArray, or NSMutableArray? Because in my code (depending on the case) I must set different background fo开发者_运维技巧r each UIImageView. How can I organize them in my code?


You don't want to put them all in an array. What you actually want to do is assign 'tags' to each view. Then, you can use -[UIView viewWithTag:(int)]; to get a pointer to the image view you want to 'talk' to at the time. Keep in mind not to use negative numbers or 0 when tagging your views.


Although I would suggest drawing your timeline view using a single UIView, dynamically creating and storing views in an array is simple:

NSMutableArray *views = [NSMutableArray array];

NSUInteger viewCount = 30;
NSUInteger index;

for (index = 0; index < viewCount; index++) {
   UIImageView *newView = [[[UIImageView alloc] initWithImage:anImage] autorelease];

   [newView setFrame:CGRectMake(index * width, 0.0, width, height);

   [[self someView] addSubview:newView];
   [views addObject:newView];
}

Remember to retain or copy views at some point.

Then, to modify a view:

[[views objectAtIndex:8] setImage:anotherImage];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜