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



我正在做一项作业,我需要在每个单元格的uitableview上显示图像(即电影海报)。我正在使用 JSON 解析获取图像的 url 数组。我正在尝试在 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]),我在其中获取填充数组的键海报路径的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开发的新手。提前谢谢。

让我指出,您不应该在cellforRowatIndexPath重新加载表-[tableviewreload]是调用一系列tableview委托方法的触发器,包括cellforRowatIndexpath(这是针对表中的每一行执行的 - 所以如果你有100行 - 这将执行100倍),在该位置进行调用编码应该会导致循环,[这可能是您的例外,尽管这似乎是一个非常常见的错误,Apple 可能已经为此类错误编写了处理程序]

我发现您的 scrollViewdidscroll 没有任何问题,因为它只是收集数据而不执行任何其他操作,并且您的 [loaddata 页码] 没有显示。

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

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

它遵循scrollViewDidScroll然后loadData,它进入内部(向后读取跟踪,即向上)。 因此,在该方法中查找insertObject,并且在某一时刻,您传递它的第一个参数是 nil。既然你没有发布那个代码,这多少我能帮上什么忙。如果您需要更多帮助,请回复。

Z.

__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;
}];

最新更新