Ios 索引1超出空数组错误的界限

Ios 索引1超出空数组错误的界限,ios,iphone,Ios,Iphone,我已经在viewDidLoad中初始化了NSMutableArray playUrl = [[NSMutableArray alloc]init]; 但是当我们在cellForRowAtIndexPath中使用 [playUrl insertObject:[NSString stringWithFormat:@"%@",_urlToLoad] atIndex:indexPath.row]; 然后将显示此错误 2016-11-21 18:06:14.481 TraidingWins[13917

我已经在viewDidLoad中初始化了NSMutableArray

playUrl = [[NSMutableArray alloc]init];
但是当我们在cellForRowAtIndexPath中使用

[playUrl insertObject:[NSString stringWithFormat:@"%@",_urlToLoad] atIndex:indexPath.row];
然后将显示此错误

2016-11-21 18:06:14.481 TraidingWins[13917:238534] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 1 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000106348d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000105dbcdeb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000106209cc5 -[__NSArrayM insertObject:atIndex:] + 901
    3   TraidingWins                        0x0000000102dc7bd6 __45-[YouTubeVC tableView:cellForRowAtIndexPath:]_block_invoke_2 + 902
    4   TraidingWins                        0x0000000102dbb840 __58+[HCYoutubeParser h264videosWithYoutubeURL:completeBlock:]_block_invoke_2 + 48
    5   libdispatch.dylib                   0x000000010705fd9d _dispatch_call_block_and_release + 12
    6   libdispatch.dylib                   0x00000001070803eb _dispatch_client_callout + 8
    7   libdispatch.dylib                   0x00000001070681ef _dispatch_main_queue_callback_4CF + 1738
    8   CoreFoundation                      0x00000001062a20f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    9   CoreFoundation                      0x0000000106263b99 __CFRunLoopRun + 2073
    10  CoreFoundation                      0x00000001062630f8 CFRunLoopRunSpecific + 488
    11  GraphicsServices                    0x00000001087f5ad2 GSEventRunModal + 161
    12  UIKit                               0x0000000104435f09 UIApplicationMain + 171
    13  TraidingWins                        0x0000000102dcba8f main + 111
    14  libdyld.dylib                       0x00000001070b492d start + 1
    15  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

有人能帮我解决崩溃问题吗?

初始化后立即将静态数据添加到数组中。如果在UITableView中添加或插入方法,将导致崩溃。这样做并检查响应

我猜您正在点击第二个对象意味着objectAtInxex 1,因此,第一个索引中没有对象,您直接在第一个索引中插入对象。我认为,如果只添加或替换URL对象列表,可以使用addObject(addbojectAtIndex特定索引)。如果你有什么特别想知道的,也请告诉我。非常感谢:)

有两种可能的解决方案:

1。您可以使用
NSMutableDictionary

初始化:

playUrl = [[NSMutableDictionary alloc] init];
单元格中,按行索引路径

playUrl[@(indexPath.row)] = [NSString stringWithFormat:@"%@", _urlToLoad];
2.看起来您的
UITableView
最初有点滚动。因此,它请求的第一个单元格是1(而不是预期的0)。如果是0点,碰撞就会消失

只能将对象插入到可用位置。例如,如果数组包含3个对象,则可以插入到0…3(包括0和3)

更新

每次将要向数组插入对象时,都需要检查索引中是否已有内容。如果存在对象,则不应插入,而应替换(
replaceObjectAtIndex:withObject:
)。否则,秩序就会被破坏

示例

用户向下滚动至url3

@[ @"url1", @"url2", @"url3" ]  // everything is fine
然后向上滚动。和表再次请求url2

@[ @"url1", @"url2", @"url2", @"url3" ]  // second url2 and url3 occupy wrong positions

如果始终插入对象,则会发生此问题。如果您将url2替换为url2,一切正常,数组不会更改。

请检查您的数组是否为空,这就是您发生崩溃的原因,打印正在处理播放的数组URL阅读错误消息,修复错误基本上就这么简单。永远不要在cellForRowAtIndexPath中执行数组插入和删除操作始终遵循最佳实践是的,我的数组是空的,我没有从数组中获取值,实际上我想插入数据。所以我不明白为什么它会崩溃。@HariMohan它崩溃是因为在向数组中添加对象时无法跳过丢失的位置。如果数组为空,则可以插入的唯一位置为0。插入到第一个位置会崩溃。1)
cellforrowatinexpath
与点击无关。2)
addObject
不够好,如果向上滚动,URL将与索引不对应;3) 问题中使用了
addbojectAtIndex
(正确的是
insertObject:atIndex:
)。