Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在数组中循环,索引错误_Ios_Objective C_Uiscrollview_Nsarray_Nsrangeexception - Fatal编程技术网

Ios 在数组中循环,索引错误

Ios 在数组中循环,索引错误,ios,objective-c,uiscrollview,nsarray,nsrangeexception,Ios,Objective C,Uiscrollview,Nsarray,Nsrangeexception,我正在创建一个横向滚动的UIScrollView,它有许多按钮和标签(与按钮一起),我正在以编程方式添加这些按钮和标签 我有44个按钮和标签,所以我想通过数组创建它们 这就是我试图做到的(我是一个新的程序员,所以我知道这是一个错误的方法) 请记住,nameArray保存标签字符串,picArray保存图片文件名字符串 无论如何,这是在我的viewDidLoad:中: for (int i = 0; i < 44; i++) { NSIndexPath *path = [nameAr

我正在创建一个横向滚动的
UIScrollView
,它有许多按钮和标签(与按钮一起),我正在以编程方式添加这些按钮和标签

我有44个按钮和标签,所以我想通过数组创建它们

这就是我试图做到的(我是一个新的程序员,所以我知道这是一个错误的方法)

请记住,
nameArray
保存标签字符串,
picArray
保存图片文件名字符串

无论如何,这是在我的
viewDidLoad:
中:

for (int i = 0; i < 44; i++) {
    NSIndexPath *path = [nameArray objectAtIndex:i];
    [self makeLabelsAndButtons:path];
    //NSLog(@"index path is:%@", path);
}
makeLabelsAndButtons
方法中没有太多内容,因为我试图在添加代码的内容之前把概念记下来

这是我崩溃时遇到的错误:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 54564 beyond bounds [0 .. 43]
我知道这意味着数组超出了边界,但我不知道为什么


非常感谢您的帮助!

如果nameArray为标签保存字符串,那么path不是NSIndexPath,而是字符串。因此,您将字符串传递给objectAtIndex:当然,它需要的是整数而不是字符串

即使是NSIndexPath,您的代码仍然是错误的,因为您希望传递到objectAtIndex的是整数而不是索引路径

我想你要做的就是把我传递给你的makeLabelsAndButtons方法

for (int i = 0; i < 44; i++) {
    [self makeLabelsAndButtons:i];
    //NSLog(@"index path is:%@", path);
}

-(void)makeLabelsAndButtons:(NSInteger)index{
    NSString *nameString = [nameArray objectAtIndex:index];
    NSString *picString = [picArray objectAtIndex:index];

    NSLog(@"name: %@, pic:%@", nameString, picString);
}
for(int i=0;i<44;i++){
[自制标签和按钮:i];
//NSLog(@“索引路径为:%@”,路径);
}
-(void)makeLabelsAndButtons:(NSInteger)索引{
NSString*nameString=[nameArray objectAtIndex:index];
NSString*picString=[picArray objectAtIndex:index];
NSLog(@“名称:%@,图片:%@”,名称字符串,图片字符串);
}
像这样试试

for (NSIndexPath *path in nameArray) {
    [self makeLabelsAndButtons:path];

}
要么这个

for (int i = 0; i < [nameArray count]; i++) {
    NSIndexPath *path = [nameArray objectAtIndex:i];
    [self makeLabelsAndButtons:path];

}

我试着像那样使用I,因为这也是我的第一直觉,但我得到了错误,“ARC不允许将'int'隐式转换为'nsindepath*”当我打开ARC时,我可以不这样做吗?@tyler53,是的,但是你不应该使用nsindepath,只使用NSInteger。然后它就会工作。你的答案和下面的答案都有帮助,谢谢很多人:)
for (NSIndexPath *path in nameArray) {
    [self makeLabelsAndButtons:path];

}