嗨,我是ios的初学者,在我的项目中我实现了水平TableList
确定一切正常,我可以创建水平TableList加载静态数据
但是我现在想要从数组列表中加载数据这个我试过下面的代码但它显示异常[__NSArrayI objectAtIndex:]:索引5超出边界[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;
}
答案 0 :(得分:1)
您的for循环条件错误。
for (int i = 0; images.count; ++i)
应该是
for (int i = 0; i < images.count; i++)