[英]while scrolling of uitableview the images are not loaded using sdwebImage
我正在做一個作業,需要在每個單元格中的uitableview上顯示圖像(電影的海報)。我正在使用JSON Parsing獲取圖像的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上重新加載表- [tableview reload]是觸發調用包括tableforRowatIndexpath的一系列tableview委托方法的觸發器(對表中的每一行都執行-因此,如果您有100行-這將執行100x),在該位置進行的調用編碼將導致循環, [這可能是您的例外,盡管這似乎是一個非常普遍的錯誤,Apple可能已為此類錯誤編碼了處理程序] !
我發現您的scrollViewdidscroll沒有任何問題,因為它只是在收集數據而沒有做其他任何事情,並且不會顯示[loaddata Pagenumber]。
您的問題可能在其他地方,請參閱堆棧跟蹤。 您的錯誤說明了問題所在
***-[__ NSArrayM insertObject:atIndex:]:對象不能為nil
它跟隨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;
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.