【发布时间】:2010-11-23 11:37:36
【问题描述】:
我有这段代码用于创建要在视图上显示的图像数组。我视图中的大多数页面将有 20 个图像(5 列,4 行),但我实际上有 43 个图像,当我的图像数组包含最后 3 个时,我在内部的第 4 次迭代时出现异常循环,数组为空。
- (void)displayImages:(NSMutableArray *)images {
NSMutableArray *keysArray = [[NSMutableArray alloc] initWithCapacity:5];
for (int column = 0; column < 5; column++){
[keysArray addObject:[NSMutableArray arrayWithCapacity:5]];
for (int row = 0; row < 4; row++) {
[[keysArray objectAtIndex:column] addObject:[images objectAtIndex:0]];
[images removeObjectAtIndex:0];
}
}
....
我可以解决这个问题吗?
谢谢。
编辑:
在此代码之后,是实际从数组中提取图像的代码。但是同样的情况发生了......在第四次迭代中它崩溃了。
for (int column = 0; column < 5; column++) {
for (int row = 0; row < 4; row++){
UIButton *keyButton = [UIButton buttonWithType:UIButtonTypeCustom];
keyButton.frame = CGRectMake(column*kKeyGap, row*kKeyGap, kKeySize, kKeySize);
[keyButton setImage:[[keysArray objectAtIndex:column] objectAtIndex:row] forState:UIControlStateNormal];
[keyButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:keyButton];
}
}
[keysArray release];
此时我无法测试images,因为数组已经清空了。
【问题讨论】:
标签: objective-c arrays exception ios4