1

I am trying to add images that have been stored to a NSMutableArray. As loop through, I can see each image but when I try to add them to my images array, I get a count of 0.

I have the following code:

.h

#import <UIKit/UIKit.h>

@class Schedule;

@interface PMCViewController : UIViewController  <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) NSMutableArray *images;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) Schedule *schedule;

- (IBAction)OpenAdmin:(id)sender;
- (IBAction)OpenSchedules:(id)sender;

@end

and them in my implementation file:

.m

- (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];

NSArray *schedules = [[ScheduleStore sharedStore] allSchedules];

scrollView.delegate = self;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;

int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80); 

int xOffset = 0;

for (int i=0; i < [schedules count]; i++) {

    Schedule *selectedSchedule = [schedules objectAtIndex:i];
    NSString *ik = [selectedSchedule imageKey];

    UIImageView *img = [[UIImageView alloc] init];
    //img.bounds = CGRectMake(10, 10, 50, 50);
    //img.frame = CGRectMake(5+xOffset, 0, 50, 50);
    //img.backgroundColor = [UIColor blackColor];
    //img.image = [[ScheduleImageStore sharedStore] imageForKey:ik];

    [img setBounds:CGRectMake(10, 10, 50, 50)];
    [img setFrame:CGRectMake((5+xOffset), 0, 50, 50)];
    [img setBackgroundColor:[UIColor blackColor]];
    [img setImage:[[ScheduleImageStore sharedStore] imageForKey:ik]];

    [images addObject:img];
    scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,50); 
    [scrollView addSubview:[images objectAtIndex:i]];

    xOffset += 70;

}
NSLog(@"Number of img added: %i",[images count]);

[[self scheduleView] reloadData];
}

I tried both dot notation and regular notation but that didn't make a difference. I can't seem to add items to the images array. Could this be a memory issue? Any ideas?

3
  • 1
    images is a weak property, how are you assigning it? Where is this mutable array coming from? Did you remember to alloc/init it? Commented May 16, 2012 at 17:28
  • Ahh, I feel foolish. Of course, and thanks for the 2nd pair of eyes. Commented May 16, 2012 at 17:32
  • When debugging follow the evidence. Adding images doesn't work so put a breakpoint at where you add images to the array and check the variables. At this point it would have been obvious since images would have been nil. So when something goes wrong, don't panic, just follow the bread crumbs. The more you do this the easier it gets. Commented May 16, 2012 at 17:58

1 Answer 1

1

you need to initialise images array.

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

Comments

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.