0

in my project I use a NSmutablearray that I fill with UIImageView in .m (viewDidLoad)

arrayView = [[NSMutableArray alloc] initWithObjects: image1, image2, image3, nil];

but in method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

when I write

int i = 1;
[[arrayView objectAtIndex:i] setImage:image];

there is an exception that say that my array is empty...why?

3
  • It's named arrayView in one place and arraySezioniView in the other? Commented Apr 13, 2011 at 16:09
  • How do you access your array, is it a property? Commented Apr 13, 2011 at 16:10
  • please post all of viewDidLoad Commented Apr 14, 2011 at 6:11

2 Answers 2

1

Please post the rest of viewDidLoad. I want to see how image1 is initialized. The line:

arrayView = [[NSMutableArray alloc] initWithObjects: image1, image2, image3, nil];

will return an empty array if image1 is nil. You should also protect your app from crashing by not making assumptions about the data source. You shouldn't crash if the array is empty, just display an empty tableView.

[EDIT]

I just read the last comment you made in the other answer. Sounds like your imageViews are created in IB. Make sure they are connected to the image1 etc outlets in IB.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this instead:

if (i < [arrayView count]) {
    UIImageView *imageView = [arrayView objectAtIndex:i];
    imageView.image = image;
}

Separating accessing the array element (by assigning it to an actual UIImageView object) and then assigning the new image may be helpful. I've seen cases where if you stack up too many operations things get confused, especially if you are dealing with objects of different types that may or may not have the selectors you're using.

Why your array is turning up empty is another issue. Initializing it in viewDidLoad seems right. You may need to add some "protection" (as above) in your table methods to avoid accessing an empty array. Then in method like viewWillAppear:, call reloadData.

11 Comments

I try but it dont' work. If I use an array of string it work well, but it don't work with an array of UIViewImage...why?
How are you creating the images you are putting into the array? Perhaps they are getting released behind your back. But I would not then expect the array to be empty. Rather, if that were happening, the array would contain references to objects that no longer exist, which would cause a crash eventually when you tried to access those objects.
IBOutlet UIImageView *image1, *image2, *image3; (in .h)
after I put the image in the array in this mode : image1 = [arrayView objectAtIndex:0]; (in viewdidload)
Oh. So the images are outlets connected with your view. Aha! I bet image1, et al are nil at the point at which you are adding them to your array. So, you are not adding anything to the array, which explains why the array is empty in cellForRowAtIndexPath. Double check that your outlets are actually connected to the view elements. You may need to initialize this array in viewWillAppear: because at that point, the outlets should definitely be available for use.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.