0

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;
}
4
  • Show the stacktrace and the code accessing the array. Also you should investigate Objective-C literal syntax; that will cut your code size in half. Commented Dec 4, 2015 at 7:30
  • please explain me with some code Commented Dec 4, 2015 at 7:44
  • 1
    Please provide the details I have requested. Commented Dec 4, 2015 at 7:58
  • sorry trojanfoe i just done here for loop mistack Commented Dec 4, 2015 at 9:00

1 Answer 1

1

Your for loop condition is wrong.

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

it should be

for (int i = 0; i < images.count; i++)
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.