2016-07-07 10 views
0

私はテーブルビューと画像をセルに持っています。 heightForRowAtIndexPath(同じ高さではない画像)を返そうとすると問題が発生しますダウンロード後の画像

これは私のクラスです。スクロールしようとするとクラッシュします。 "2016-07-07 21:44:16.990 xem.vn [1725:25586] *キャッチされていない例外 'NSRangeException'、理由: '* - [__ NSArrayM objectAtIndex:]:境界2を超えるインデックス2 .. 1] '"

- (void)viewDidLoad { 
    [listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"]; 
    [listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"]; 
    [listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"]; 
    [listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"]; 
    [listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"]; 
    [listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"]; 
    [listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4"]; 
    [listImage addObject:@"https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg"]; 
    [listImage addObject:@"https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/idcs1426.jpg?itok=Gc_-Q58L"]; 
    imageHeights = [[NSMutableArray alloc]init]; 
    [self setDefaultRowHeights]; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [listImage count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"newTableViewCell"; 
    newTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = (newTableViewCell *)[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil][0]; 
    } 
    NSString *imageURL = listImage[indexPath.row]; 
    __weak newTableViewCell *weakCell = cell; 
    __weak typeof(self) weakSelf = self; 
    [cell.NewsImageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]] 
           placeholderImage:[UIImage new] 
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
              weakCell.NewsImageView.image = image; 
              NSInteger oldHeight = [imageHeights[indexPath.row] integerValue]; 
              NSInteger newHeight = (int)image.size.height; 
              if (image.size.width != CGRectGetWidth(weakCell.NewsImageView.bounds)) { 
               CGFloat ratio = image.size.height/image.size.width; 
               newHeight = CGRectGetWidth(self.view.bounds) * ratio; 
              } 

              if (oldHeight != newHeight) { 
               imageHeights[indexPath.row] = @(newHeight); 
               [weakSelf.NewsTableView beginUpdates]; 
               [cell layoutIfNeeded]; 
               [weakSelf.NewsTableView endUpdates]; 
              } 
             } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
              NSLog(@"Error: %@\nFetching image with url: %@", error, request.URL); 
             }]; 
    return cell; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return [imageHeights[indexPath.row] integerValue]; 
} 
- (void)setDefaultRowHeights { 
    imageHeights = [NSMutableArray arrayWithCapacity:listImage.count]; 
    for (int i = 0; i < listImage.count; i++) { 
     imageHeights[i] = @(self.NewsTableView.rowHeight); 
    } 
} 

ありがとうございました!

+0

正確にどこがクラッシュするのかを教えてください。 – lawicko

答えて

0

私は、あなたのimageHeightslistImageのアイテム数が異なるためにクラッシュが発生したと考えています。 viewDidLoadを実行した後、にはsetDefaultRowHeightsを実行してもimageHeightsのアイテムが8個あります。

imageHeights = [NSMutableArray arrayWithCapacity:listImage.count]のみimageHeightsはのListImageと同じ数のオブジェクトを保持すると言うが、imageHeightsのカウント数はまだ私がself.NewsTableViewが正しくセットアップされている場合について不明午前0

です。正しいと仮定すると、setDefaultRowHeightsは次のように書かれています。

- (void)setDefaultRowHeights { 
    imageHeights = [NSMutableArray arrayWithCapacity:listImage.count]; 
    for (int i = 0; i < listImage.count; i++) { 
     [imageHeights addObject:@(self.NewsTableView.rowHeight)]; 
    } 
} 
+0

私はあなたのようにしようとしますが、それは同じです。あなたはURLからの画像をロードしてセルに表示するソリューションを持っていますか?ダイナミックハイト(同じサイズではない画像) –

関連する問題