在滚动uitableview时,不使用sdwebImage加载图像

时间:2014-03-09 16:07:04

标签: ios objective-c uitableview sdwebimage

我正在做一个任务,我需要在每个单元格中的uitableview上显示图像(即电影海报)。我正在使用JSON Parsing获取图像的网址数组。我试图在uitableview上显示但是对于前60部电影,它可以正确显示图像,但是当我向下滚动时,它给了我一个例外。我正在使用SDWebImage框架,如下所示。我导入了#import <SDWebImage/UIImageView+WebCache.h>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    // Configure the cell...

    tableViewCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.movieNameLabel.text = [finalTitleArray objectAtIndex:indexPath.row];
    cell.movieNameLabel.numberOfLines = 2;
    cell.releasedDateLabel.text = [finalReleasedDateArray objectAtIndex:indexPath.row];
    NSURL *urlForimageDisplay = [finalImagesArray objectAtIndex:indexPath.row];


    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:urlForimageDisplay options:0 progress:^(NSUInteger receivedSize,long long expectedSize) {}
    completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,BOOL finished){
        if (image) {
            cell.movieImage.image = image;

        }
        else{
        }
    }];


    NSString *populartyNumberLabelTextString = [NSString stringWithFormat:@"%@",    [finalPopularityNumberArray objectAtIndex:indexPath.row]];
    cell.populartyNumberLabel.text = populartyNumberLabelTextString;
                         NSString *numberOfUsersLabelText = [NSString stringWithFormat:@"%@",[finalNumberOfUsersArray objectAtIndex:indexPath.row]];

    cell.numberOfUsersLabel.text = numberOfUsersLabelText;
    return cell;
    [tableView reloadData];
}




-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

                         CGPoint offset= scrollView.contentOffset;
                         CGRect bounds = scrollView.bounds;
                         CGSize size = scrollView.contentSize;
                         UIEdgeInsets inset = scrollView.contentInset;
                         float y = offset.y + bounds.size.height - inset.bottom;
                         float h = size.height;
                         float reload_distance = 0;
                              if (y >= h + reload_distance) {
                                                               pageNumber = pageNumber + 1;
                                                               NSLog(@"CURRENT PAGE NUMBER %d",pageNumber);
                                                               [self loadData:pageNumber];
}

这是方法([self loadData:pageNumber]),其中我正在为填充数组的关键posterpath获取JSON值。它给出了如下例外:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x01aa65e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x018298b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01a58bcc -[__NSArrayM insertObject:atIndex:] + 844
    3   CoreFoundation                      0x01a58870 -[__NSArrayM addObject:] + 64
    4   customizedCellTable                 0x000032e5 -[tableViewClass loadData:] + 3029
    5   customizedCellTable                 0x00004643 -[tableViewClass scrollViewDidScroll:] + 467
    6   UIKit                               0x0062395b -[UIScrollView(UIScrollViewInternal) _notifyDidScroll] + 62
    7   UIKit                               0x0060dc43 -[UIScrollView setContentOffset:] + 734
    8   UIKit                               0x0067f1d1 -[UITableView setContentOffset:] + 314
    9   UIKit                               0x0061ead2 -[UIScrollView _smoothScrollWithUpdateTime:] + 4009
    10  UIKit                               0x0061eda9 -[UIScrollView _smoothScrollDisplayLink:] + 222
    11  QuartzCore                          0x03b5db8a _ZN2CA7Display15DisplayLinkItem8dispatchEv + 48
    12  QuartzCore                          0x03b5da46 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 310
    13  QuartzCore                          0x03b5df6b _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 123
    14  CoreFoundation                      0x01a64bd6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
    15  CoreFoundation                      0x01a645bd __CFRunLoopDoTimer + 1181
    16  CoreFoundation                      0x01a4c628 __CFRunLoopRun + 1816
    17  CoreFoundation                      0x01a4bac3 CFRunLoopRunSpecific + 467
    18  CoreFoundation                      0x01a4b8db CFRunLoopRunInMode + 123
    19  GraphicsServices                    0x039fb9e2 GSEventRunModal + 192
    20  GraphicsServices                    0x039fb809 GSEventRun + 104
    21  UIKit                               0x00597d3b UIApplicationMain + 1225
    22  customizedCellTable                 0x00005fdd main + 141
    23  libdyld.dylib                       0x023c4725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我是iphone开发的新手。提前谢谢。

3 个答案:

答案 0 :(得分:0)

让我指出你不应该在cellforRowatIndexPath重新加载你的表 - [tableview reload] 是一个触发器来调用一系列tableview委托方法,包括cellforRowatIndexpath(对每一行执行)在表中 - 所以如果你有100行 - 这会被执行100次),编码那个地方的调用应该会产生一个循环, [这可能是你的例外,虽然这似乎是一个非常常见的错误,Apple可能有这种错误的编码处理程序]

我发现你的scrollViewdidscroll没有任何问题,因为它只是在没有做任何其他事情的情况下收集数据而且你的[loaddata Pagenumber]没有显示出来。

答案 1 :(得分:0)

您的问题可能在其他地方,请参阅堆栈跟踪。您的错误说明问题是什么

  

*** - [__ NSArrayM insertObject:atIndex:]:对象不能为零

它跟随scrollViewDidScroll然后loadData,它在内部(向后读取跟踪,向上读取)。 因此,在该方法中查找insertObject,并且在某一时刻,您传递的第一个参数为nil。由于您没有发布该代码,因此我可以提供多少帮助。如果您需要更多帮助,请回来。

Ž。

答案 2 :(得分:0)

        __block UIActivityIndicatorView *activityIndicator;
        __weak UIImageView *weakImageView = yourImageView;

        [yourImageView sd_setImageWithURL:"your image url here..."
                         placeholderImage:[UIImage imageNamed:@"placeholder"]
                                  options:SDWebImageProgressiveDownload
                                 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                     if (!activityIndicator) {
                                         [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                                         activityIndicator.center = weakImageView.center;
                                         [activityIndicator startAnimating];
                                     }
                                 }
                                completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                                    [activityIndicator removeFromSuperview];
                                    activityIndicator = nil;
                                }];