如何使用for循环在NSMUtableArray中添加对象

how to add objects in NSMUtableArray using for loop

嗨,我是 ios 的初学者,在我的项目中我已经实现了水平 TableList

好的,一切都很好,我可以创建水平的 TableList 加载静态数据

但我现在想从数组列表中加载数据,为此我尝试了下面的代码,但它显示异常 [__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]' 并且当我添加时像 paidList 数组这样的对象没有错误出现但是当我使用 for 循环插入对象时它显示异常

请帮我看看我哪里做错了?

我的代码:-

#import "ViewController.h"

@interface ViewController ()
{
    NSArray * images;
    NSArray * titles;
}

@end

@implementation ViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        images = [[NSArray alloc]initWithObjects:@"5_64x64.png",@"6_64x64.png",@"7_64x64.png",@"8_64x64.png",@"9_64x64.png",nil];

        titles = [[NSArray alloc]initWithObjects:@"Weather",@"Weather",@"E-Trade",@"Voice Recorder",@"News Reader", nil];

        freeList =[[NSMutableArray alloc]init];

        for (int i = 0; images.count; ++i) {

        ListItem *item = [[ListItem alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:[images objectAtIndex:i]] text:[titles objectAtIndex:i]];

        [freeList addObject:item];

        }

        NSLog(@"free list is %@",freeList);

        ListItem *item6 = [[ListItem alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:@"10_64x64.png"] text:@"Game Pack"];
        ListItem *item7 = [[ListItem alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:@"11_64x64.png"] text:@"Movies"];
        ListItem *item8 = [[ListItem alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:@"12_64x64.png"] text:@"Forecast"];
        ListItem *item9 = [[ListItem alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:@"10_64x64.png"] text:@"Game Pack"];
        ListItem *item10 = [[ListItem alloc] initWithFrame:CGRectZero image:[UIImage imageNamed:@"10_64x64.png"] text:@"Game Pack"];

        paidList = [[NSMutableArray alloc] initWithObjects: item6, item7, item8, item9, item10, nil];

    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (int)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 155.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    NSString *title = @"";
    POHorizontalList *list;

    if ([indexPath row] == 0) {
        title = @"Top Free";

        list = [[POHorizontalList alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 155.0) title:title items:freeList];
    }
    else if ([indexPath row] == 1) {
        title = @"Top Paid";

        list = [[POHorizontalList alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 155.0) title:title items:paidList];
    }

    [list setDelegate:self];
    [cell.contentView addSubview:list];

    return cell;
}

你的for循环条件是错误的。

for (int i = 0; images.count; ++i)

应该是

for (int i = 0; i < images.count; i++)