UITableView在超出边界的NSArray索引1上崩溃

时间:2015-02-13 00:38:47

标签: ios objective-c uitableview nsarray

我的桌子一直在崩溃。 "索引1超出界限"。 我返回该部分中行数的数组计数,但只要滚动到第二行(索引1),应用程序就会崩溃。

我有两个部分。一个具有设定行数,第二个部分是根据数组中的项目数动态创建的。

有什么想法吗?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
          return 2; //two seperate areas
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        if (section == 0) {
            return 16; 
        } else {
            NSLog(@"Section: %ld, NumberOfRows: %lu", (long)section, (unsigned long)[self.arrImages count]);
            return [self.arrImages count]; 
        }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];


        switch (section) {
            case 0:

                 cell =  [super tableView:tableView  cellForRowAtIndexPath:indexPath];
                break;

            case 1:

                cell = [tableView dequeueReusableCellWithIdentifier:@"cellPhoto"];
                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellPhoto"];
                }
                NSLog(@"Row: %lu\n%@", (unsigned long)row, [[self.arrImages objectAtIndex:row] objectForKey:@"imageThumbnailURL"]);

                cell.imageView.image = [self imageFromURL:[[self.arrImages objectAtIndex:row] objectForKey:@"imageThumbnailURL"]  fromCache:YES];
                NSLog(@"completed");
                break;
        }


    return cell;
}

#pragma mark UITableViewDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger section = [indexPath section];

    [self doneData];

        switch (section) {
            case 1:
                [tableView deselectRowAtIndexPath:indexPath animated:YES];
                [self performSegueWithIdentifier:@"segue_showphoto" sender:self];
                break;
       }
}

这是我的崩溃日志

2015-02-12 19:32:51.817 Tops[17236:5681531] Row: 0
https://somethinghere.jpeg
2015-02-12 19:32:51.825 Tops[17236:5681531] completed
2015-02-12 19:32:51.855 Tops[17236:5681531] Row: 1
https://somthinghere.jpeg
2015-02-12 19:32:51.856 Tops[17236:5681531] completed
2015-02-12 19:32:51.862 Tops[17236:5681531] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

1 个答案:

答案 0 :(得分:1)

我失踪的实施是:

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { 
return 0; 
}