AbhiRam
AbhiRam

Reputation: 2061

how to add objects in NSMUtableArray using for loop

Hi i am beginner in ios in my project i have implemented horizontal TableList

ok everything is fine and i could able to create horizontal TableList loading static data

but i want load data now from array list for this i have tried below code but it showing exception [__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]' and when i add objects as like paidList array there is no errors coming but when i use for loop for inserting objects it's showing exceptions

please help me what did i do here wrong?

my code:-

#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;
}

Upvotes: 0

Views: 532

Answers (1)

Alok Rao
Alok Rao

Reputation: 162

Your for loop condition is wrong.

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

it should be

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

Upvotes: 1

Related Questions