2016-10-01 12 views
0

imageviewをテーブルビューで読み込むと問題が発生します。それはゆっくりと読み込んでいます。私はテーブルビューで多くのセクションを使用します。私のコードがあります:UITableviewの読み込みイメージを改善するには?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
TrangChuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellTrangchu" forIndexPath:indexPath]; 

theGame *thegameObj; 
if([indexPath section] == 0){ 

    thegameObj = [theZingArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 1){ 

    thegameObj = [theBitArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 2){ 

    thegameObj = [theMobayArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 3){ 

    thegameObj = [theGateArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 4){ 

    thegameObj = [theVcoinArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 5){ 

    thegameObj = [theGarenaArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 6){ 

    thegameObj = [theOncashArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 7){ 

    thegameObj = [theMobiphoneArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 8){ 

    thegameObj = [theVinaphoneArray objectAtIndex:indexPath.row]; 

}else if([indexPath section] == 9){ 

    thegameObj = [theViettelArray objectAtIndex:indexPath.row]; 

} 
NSURL *urlImage = [NSURL URLWithString:thegameObj.image]; 
NSData *imageData = [NSData dataWithContentsOfURL:urlImage]; 
[email protected]"1"; 
UIImage *image= [UIImage imageWithData:imageData]; 
cell.imageView.layer.cornerRadius=5; 
cell.imageView.layer.masksToBounds=YES; 
cell.imageView.image = image; 
cell.labelName.text = thegameObj.objectName; 

if([[AppDelegate appDelegate]checkIP]) 
{ 
    [cell.txtQuantity setBackgroundColor:[UIColor whiteColor]]; 
    [cell.txtQuantity.layer setBorderColor:[UIColor grayColor].CGColor]; 
    [cell.txtQuantity.layer setBorderWidth:1.0]; 
    [cell.txtQuantity.layer setCornerRadius:5]; 
    cell.labelPrice.text = [NSString stringWithFormat:@"%@ (%@)", [NSNumberFormatter localizedStringFromNumber:thegameObj.price numberStyle:NSNumberFormatterDecimalStyle], loadCurrency]; 

} 
else 
{ 
    [cell.lbdetail setTitle:@"detail" forState:UIControlStateNormal]; 
    cell.txtQuantity.hidden=YES; 
    cell.labelPrice.hidden=YES; 
    cell.lbgia.hidden=YES; 
    cell.lbsl.hidden=YES; 
    cell.lbdetail.hidden=YES; 

} 

cell.objArray = thegameObj; 
cell.currency = loadCurrency; 
/* 
[cell.addToCartButton addTarget:self action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside]; 
cell.addToCartButton.tag = [NSString stringWithFormat:@"%d_%d", (NSInteger)[indexPath section], (NSInteger)[indexPath row]]; 
*/ 
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 


return cell; 
} 

のUITableViewでセクションの多くに、より速くロードイメージを向上させる助けてください。何か解決していただきありがとうございます!

答えて

2

1つのことは、配列の配列である断面付きテーブルモデルに一致するデータ構造です。あなたはサブアレイのすべてを持っているので、ほぼそこにいます。このようなものを構築します(すべてのサブ配列は、ないcellForRowで構築されている場合):

thegameObj = model[indexPath.section][indexPath.row]; 

あなたが他の場所であることを使用することができます:あなたは1行に大きな条件を平らにできるようになる

NSArray *model = @[ theZingArray, theBitArray, /*... and so on */]; 

numberRowsInSection

NSArray *sectionArray = model[indexPath.section]; 
return sectionArray.count; 

のようにやや厳しい問題は、これらの画像は、非同期ロードされ、キャッシュを取得することです。完全にネイティブなアプローチについては、hereと多くのothersについて説明しています。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"]; // fixme 

    theGame *thegameObj = model[indexPath.section][indexPath.row]; 
    NSString *urlString = thegameObj.image; 
    UIImageView *imageView = cell.imageView; 

    [self imageAtURLString:urlString completion:^(UIImage *image, BOOL wasCached) { 
     if (wasCached) imageView.image = image; 
     else [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    }]; 

    return cell; 
} 

あなたのコードにこのアイデアを適用し、ここでどちらかのキャッシュされた画像を返し、または、キャッシュとリターンをフェッチする方法...

// declare this and BE SURE TO INITIALIZE IT 
@property(strong,nonatomic) NSMutableDictionary *imageCache; 

// return an image found at the url string, if one is cached return it, otherwise, 
// fetch it, cache it and return it 
- (void)imageAtURLString:(NSString *)urlString completion:(void(^)(UIImage *, BOOL))completion { 
    if (self.imageCache[urlString]) { // if it's cached 
     completion(self.imageCache[urlString], YES); 
    } else { // fetch and cahce 
     NSURL *url = [NSURL URLWithString:urlString]; 

     NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
      if (data) { 
       UIImage *image = [UIImage imageWithData:data]; 
       if (image) { 
        self.imageCache[urlString] = image; 
        dispatch_async(dispatch_get_main_queue(), ^{ 
         completion(image, NO); 
        }); 
       } 
      } 
     }]; 
     [task resume]; 
    } 
} 

は、今すぐあなたの簡略化cellForRowAtIndexPath次のように見ることができます

ここでは、キャッシュされたイメージを持っていることがあります。その場合は、セルのimageViewイメージを設定するか、イメージをフェッチする必要があります。フェッチの場合、フェッチ中に行がスクロールされたかどうかは不明です。現在の行をリロードして(現在キャッシュされている)イメージで更新させます。

+0

解決に感謝します! –

2

画像の表示が背景で画像を非同期に読み込む画像キャッシュの概念があります。次のリンクを参照してください https://github.com/nicklockwood/AsyncImageView

+1

キャッシングとロード非同期は明確なアイデアです。 UIImageViewに特化した多くのネットコードがあり、それは独自の読み込みであり、それは良いアイデアです(ネットコードなしでも簡単にビルド可能です)。これらのイメージビューインスタンスは自然にイメージをキャッシュしますが、スクロール中に再利用されたセルのテーブルビューではあまり効果がありません。 – danh

0

バックグラウンドで画像をダウンロードするには、次の素晴らしいライブラリを使用してください。

https://github.com/rs/SDWebImage

UIImageView * thumbnail = [[UIImageView alloc] init]; 
NSURL *urlToPicture1 = [NSURL URLWithString:currentMarker.thumbnail]; 

[thumbnail sd_setImageWithURL:urlToPicture1 placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload completed:^(UIImage * image, NSError * error,SDImageCacheType cachedType, NSURL * imageURL){ 
    if(image){ 
     [thumbnail setImage:image]; 
     NSLog(@"Complete = %d, error = %@",cachedType, error); 
    } 
}]; 
+0

画像読み込み作業を画像ビューに移動するのはいいアイデアですが、それはuitableviewセルに含まれる画像ビューには不十分です。ビューが再利用されるため、ビューのキャッシュが低すぎます。 – danh

+0

再利用セルの解決策を確認できます:http://stackoverflow.com/questions/23741124/sdwebimage-uitableviewcell-wrong-image-when-scrolling – KKRocks

関連する問題