iOS错误:[__ NSArrayI objectAtIndex:]:超出边界的索引1 [0 .. 0]

时间:2012-09-28 13:33:36

标签: ios uitableview nsmutablearray indexoutofboundsexception

在我的应用程序中,我尝试从url加载内容,将它们存储在一个可变数组中并在表视图中显示它们。但是我无法使其正常运行,因为每次运行应用程序时都会出现此错误:

*** Terminating app due to uncaught exception 'NSRangeException', 
reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x34dd088f 0x36d9e259 0x34d2823d 0x316e562f 0x315f09a9 0x313c0c5d 0x313c1b95 0x313c1ae7
 0x313c16c3 0xa5cfb 0x33623ec7 0x35387a09 0x35390051 0x33622965 0xa4dc1 0x313a8e33 
 0x313cd629 0x31391d7d 0x314544dd 0x3139a55d 0x3139a579 0x3139a40b 0x3139a3e7 0x313a8015 
 0x313a1985 0x3136fc6b 0x3136f70f 0x3136f0e3 0x3439222b 0x34da4523 0x34da44c5 0x34da3313 
 0x34d264a5 0x34d2636d 0x313a0a13 0x3139de7d 0xa4745 0xa46dc)
terminate called throwing an exception

我创建了一个数组,该数组应该在viewDidLoad中填充表:

_videos = [[NSMutableArray alloc] init];

然后我连接到url并解析收到的xml数据。这样就可以了。当打开某个标签时,我创建了我的视频对象,在用数据填充之后,我将这些对象添加到我的数组中:

[_videos addObject:currentVideo];

这似乎也有效,因为它会在

时返回正确数量的视频
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _videos.count;
}

被调用。但在此之后,应用程序崩溃,我甚至无法达到我尝试填充表格视图的程度。该函数如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    Video *curVideo = [_videos objectAtIndex:indexPath.row];

    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)        
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    cell.titleLabel.text = [curVideo title];
    cell.descLabel.text = [curVideo desc];

    return cell;
}

出了什么问题?

提前致谢

3 个答案:

答案 0 :(得分:5)

在尝试呈现模式故事板时,我有相同的错误签名-[__NSArrayI objectAtIndex:]: index 4 beyond bounds [0 .. 1]',表视图有两个(从五个更新)静态表格单元格部分。直到我删除了视图中不再需要的三个表格单元格部分才出现此错误。在模态演示文稿之前检查我的所有objectAtIndex引用两天后,我决定查看UITableViewController子类代码本身。我发现了这个:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 5;
}

然后灯泡熄灭了。被引用的index 4与我的表视图段数和我当前的两个表格单元格部分引用的[0 .. 1]的界限相关。更新return值以匹配Storyboard表视图中当前表格单元格部分的数量解决了该问题。

答案 1 :(得分:4)

在初始化之前,您可能会访问_videos。很可能你是在init之后但在加载视图之前做的。解决此类问题的方法是仅使用访问器,并使用惰性初始化self.videos。除了initdealloc之外,这是直接访问您的ivars的众多原因之一。

@interface ...
@property (nonatomic, readonly, strong) NSMutableArray *videos;
@end

@implementation ...
{
  NSMutableArray *_videos; // Can't auto-synthesize. We override the only accessor.
}

- (NSMutableArray *)videos {
   if (! _videos) {
     _videos = [NSMutableArray new];
   }
   return _videos;
}

现在,无论何时发生,都会初始化对self.videos的所有引用。

您还可以在init中正确初始化视频,这需要的代码少一些:

@interface ...
@property (nonatomic, readonly, strong) NSMutableArray *videos;
@end

@implementation ...

- (id)init {
   self = [super init];
   if (self) {
     _videos = [NSMutableArray new];
   }
   return self;
}

答案 2 :(得分:0)

我已经传递了像这样的图像数组的虚拟图像名称,

arrImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"some.png"]

以上一行导致我错误。所以我用@“category.png”这样的现有图像改变了@“some.png”。

这对我有用。确保从包中传递正确的图像名称。

相关问题