1

我正在尝试添加已存储到 NSMutableArray 的图像。在循环中,我可以看到每个图像,但是当我尝试将它们添加到我的图像数组中时,我得到的计数为 0。

我有以下代码:

。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

并将它们放在我的实现文件中:

.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];
}

我尝试了点表示法和常规表示法,但这并没有什么不同。我似乎无法将项目添加到图像数组中。这可能是内存问题吗?有任何想法吗?

4

1 回答 1

1

你需要初始化images数组。

于 2012-05-16T17:33:32.580 回答