2016-06-01 73 views
-1

我有一個平滑的滾動問題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]; 

    }//[email protected]"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 *[email protected]"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; 

} 
+0

問題是什麼? – solarissmoke

+1

問題在於圖像下載和分配。這兩件事情都發生在主線程上,因爲你在滾動中面臨滯後。使用一些第三方庫進行圖像下載和設置。 –

回答

0

首先安裝莢:

莢 '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]; 

}//[email protected]"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

+0

問題是因爲您正在同步加載圖像 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'方法。使用SDWebImage/WebP異步加載圖像。現在你將不會面臨滾動問題。 –

1

如果您在cellForRowAtIndexPath加載圖像,使用異步方法backgroundly加載圖片時,使用此下面的代碼,

dispatch_queue_t image_queue = dispatch_queue_create("com.company.app.imageQueue", NULL); 
    dispatch_queue_t main_queue = dispatch_get_main_queue(); 

    NSString *[email protected]"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,它的工作正常滾動,希望它有幫助

+0

謝謝你的有用信息 –

+0

@priyasharma這個答案是正確的,適合你的工作,請選擇答案 –