2017-08-22 22 views
-3

Webサービスからのデータが大量で、プロジェクトにコレクションビューがあります。コレクションビューを垂直方向にスクロールするとスムーズにスクロールしません。UICollectionViewがスムーズにスクロールしない

SDWebImageを使用して、画像を非同期に読み込みました。ここで

cellForItemAtIndexPathのコードです:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CategoryListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryListCell" forIndexPath:indexPath]; 

    cell.movName.text = [nameArray objectAtIndex:indexPath.item]; 

    num = [[numArray objectAtIndex:indexPath.item]integerValue]; 
    cell.itemNo.text = [NSString stringWithFormat:@"%ld", (long)num]; 


    [cell.movImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imageArray objectAtIndex:indexPath.item]]] placeholderImage:[UIImage imageNamed:@"Main_Logo.png"]]; 
    [cell.movImage setShowActivityIndicatorView:YES]; 
    [cell.movImage setIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 

    return cell; 
} 

私はシミュレータ上でプロジェクトを実行すると、それがスムーズにスクロールするが、物理デバイス上でそれを実行したときに、それがスムーズにスクロールしません。これをどうすれば解決できますか?

+0

は、私はあなたが時間プロファイラ計測器 – Paulw11

+0

私は、あなたがcellForItemAtメソッド内のループのために必要ないけないと思いますが使用することをお勧めだけで使用NUM = [[numArray objectAtIndex:indexPath.item]するIntegerValue]。 cell.itemNo.text = [NSString stringWithFormat:@ "%ld"、(long)num]; forループは一切ありません – 3stud1ant3

+0

私はすでにそれを使用しましたが、解決策Paulは見つかりませんでした。 – MUNNA

答えて

2

for loopは、その大きさに基づいて、犯人のように見えます。 SDWebImageはバックグラウンドスレッド上で操作を行うので、大丈夫です。

ただし、メソッドcollectionView:cellForRowAtIndexPath:のプロファイリングを行い、各セルのレンダリングにかかる​​時間を計算します。 0.016秒を超えると、パフォーマンスが遅くなる理由です。以下は、すぐにセルレンダリングのパフォーマンスを測定するために使用できるコードです。

NSDate *methodStart = [NSDate date]; 

CategoryListCell *cell = [[CategoryListCell alloc]init]; 
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryListCell" forIndexPath:indexPath]; 

cell.movName.text = [nameArray objectAtIndex:indexPath.item]; 

for (int i=0; i<=numArray.count; i++) 
{ 
    num = [[numArray objectAtIndex:indexPath.item]integerValue]; 
    cell.itemNo.text = [NSString stringWithFormat:@"%ld", (long)num]; 
} 


[cell.movImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imageArray objectAtIndex:indexPath.item]]] placeholderImage:[UIImage imageNamed:@"Main_Logo.png"]]; 
[cell.movImage setShowActivityIndicatorView:YES]; 
[cell.movImage setIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 

NSDate *methodFinish = [NSDate date]; 
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart]; 
NSLog(@"executionTime = %f", executionTime); 

この方法の迅速なプロファイリングの詳細については、この点を確認してください。How to log a method's execution time exactly in milliseconds?

+0

スクロールのパフォーマンスを示すgifや何かを投稿して、このUICollectionViewの周りにコードを投稿することはできますか? – prabodhprakash

0

は、私が思うに、あなたの間違いラインコードfor.Therefore、それはcollectionViewに滑らかではないでしょう。バックグラウンドで設定するか、関数cellForItemAtIndexPathの外で計算できます。

+0

しかし、forループを削除した後も同じです – MUNNA

0

私は本当に多くの問題を認識していません。私が指摘したいカップルのもの。

  1. cellForItemAtIndexPathでコレクションビューセルを不必要に開始しています。 dequeueReusableCellWithReuseIdentifier:forIndexPath:常にセルを返します。

  2. cellForItemAtIndexPathのforループは何をしますか?それはitemNo text numArray count timesを設定しています。私は誤読ですか?

  3. movimageに角がありますか?次に、丸い角を使わずに試してみてください。丸みを帯びたコーナーが重い。

+0

はい。私はループを削除し、movImageには角がありません。 – MUNNA

関連する問題