表视图的平滑滚动
我有一个平滑的UITableView
滚动问题。 任何人都可以帮我解决问题吗? 我写的代码是这样的 –
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomTableCell"; LabTestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; }//cell.txtHeading.text=@"hello"; cell.txtHeading.text=[[_ResponseDic valueForKey:@"title"]objectAtIndex:indexPath.row]; cell.txtdescribtion.text=[[_ResponseDic valueForKey:@"description"]objectAtIndex:indexPath.row]; cell.txtPrice.text=[[_ResponseDic valueForKey:@"purchase_price"]objectAtIndex:indexPath.row]; cell.txtLabName.text=[[_ResponseDic valueForKey:@"brand"]objectAtIndex:indexPath.row]; NSString *path=@"https://www.fingerfry.com/pharmgo/uploads/product_image/"; NSString *url=[path stringByAppendingString:[[_ResponseDic valueForKey:@"main_image"]objectAtIndex:indexPath.row]]; NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]]; UIImage* image = [[UIImage alloc] initWithData:imageData]; cell.image.image=image; return cell; }
首先安装pod:
在pod文件中的pod'SDWebImage / WebP','〜> 3.7'。
那么你必须导入“。
#import <SDWebImage/UIImageView+WebCache.h>
写下面的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomTableCell"; LabTestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil]; cell = [nib objectAtIndex:0]; }//cell.txtHeading.text=@"hello"; cell.txtHeading.text=[[_ResponseDic valueForKey:@"title"]objectAtIndex:indexPath.row]; cell.txtdescribtion.text=[[_ResponseDic valueForKey:@"description"]objectAtIndex:indexPath.row]; cell.txtPrice.text=[[_ResponseDic valueForKey:@"purchase_price"]objectAtIndex:indexPath.row]; cell.txtLabName.text=[[_ResponseDic valueForKey:@"brand"]objectAtIndex:indexPath.row]; [cell.image sd_setImageWithURL:[NSURL URLWithString:[obj valueForKey:IMAGE_KEY]] placeholderImage:nil options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { }]; return cell; }
参考链接: https : //github.com/rs/SDWebImage
如果在cellForRowAtIndexPath
加载图像,则使用Asynchronous方法背景加载图像,使用下面的代码,
dispatch_queue_t image_queue = dispatch_queue_create("com.company.app.imageQueue", NULL); dispatch_queue_t main_queue = dispatch_get_main_queue(); NSString *path=@"https://www.fingerfry.com/pharmgo/uploads/product_image/"; NSString *url=[path stringByAppendingString:[[_ResponseDic valueForKey:@"main_image"]objectAtIndex:indexPath.row]]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachePath = [paths objectAtIndex:0]; NSString *dataPath = [cachePath stringByAppendingPathComponent:url]; NSFileManager *fileManager = [NSFileManager defaultManager]; if ( [fileManager fileExistsAtPath:dataPath] ) { UIImage *image = [UIImage imageWithContentsOfFile:dataPath]; [cell.imgView setImage:image]; } else { [cell.imgView setImage:[UIImage imageNamed:@"images.png"]]; dispatch_async(image_queue, ^{ NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; UIImage *image = [UIImage imageWithData:imageData]; [UIImagePNGRepresentation(image) writeToFile:dataPath atomically:YES]; dispatch_async(main_queue, ^{ [cell.imgView setImage:image]; }); }); }
我有同样的问题,我把上面的代码在cellForRowAtIndexPath
,其工作正常滚动,希望它的帮助