2014-02-15 66 views
1

我的目標是使用UICollectionView顯示一行延遲加載的縮略圖(數字未知),然後用戶可以水平滾動縮略圖。防止UICollectionView重繪

我使用下面的代碼來實現接近我後,但我得到一個錯誤。

前4個縮略圖加載沒關係,但是當您滾動縮略圖時,會開始按照對方的頂部和隨機順序繪製。

我想獲得它,使它達到如下:

Item 1 ----- Item 2 ----- Item 3 ---- Item 4 

我想它使細胞只畫一次類似的,當你使用一個UITableView你會得到什麼。我只希望UICollectionView在我要求時重新加載數據。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; 

UIView *tmpView; 

if([self.selectedEffects isEqualToString:@"Effects"]){ 

    int index = indexPath.row; 

    NSLog(@"%i",index); 

    tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 90)]; 
    [tmpView setBackgroundColor:[UIColor clearColor]]; 

    UIImageView *imageViewWithEffect = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 60, 60)]; 
    imageViewWithEffect.layer.cornerRadius = 10.00; 
    imageViewWithEffect.clipsToBounds = YES; 

    UILabel *labelEffect = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, 70, 20)]; 
    [labelEffect setText:[[self.activeEffectsArray objectAtIndex:index] objectAtIndex:1]]; 
    labelEffect.textAlignment = NSTextAlignmentCenter; 
    labelEffect.textColor = [UIColor whiteColor]; 
    [labelEffect setFont:[UIFont boldSystemFontOfSize:8]]; 

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 

    [activityIndicator setFrame:CGRectMake(0, 0, 60, 60)]; 
    activityIndicator.alpha = 1.0; 
    [activityIndicator startAnimating]; 

    UIButton *buttonSelect = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [buttonSelect setFrame:CGRectMake(0, 0, 60, 90)]; 
    [buttonSelect setTag:index]; 
    [buttonSelect addTarget:self action:@selector(applyEffects:) forControlEvents:UIControlEventTouchUpInside]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ 

     UIImage *imageWithEffect = [self editImage:[[self.activeEffectsArray objectAtIndex:indexPath.row] objectAtIndex:0] ImageToEdit:self.thumbnailImage]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      [imageViewWithEffect setImage:imageWithEffect]; 
      [activityIndicator removeFromSuperview]; 

     }); 

    }); 

    [tmpView addSubview:imageViewWithEffect]; 
    [tmpView addSubview:labelEffect]; 
    [tmpView addSubview:activityIndicator]; 
    [tmpView addSubview:buttonSelect]; 

} 

[cell addSubview:tmpView]; 

return cell; 
} 
+0

因其出隊方式而重新繪製。你不應該下載cellForItemAtIndexPath中的圖像。但下載您的圖像在卸載或其他地方,並提供該圖像imageViewWithEffect; – Pawan

回答

2

您每次都向單元添加子視圖。這是不正確的,因爲單元格可能已被重用。檢查子視圖是否已經創建,如果它們已經存在,請進行修改。你可以通過繼承UICollectionViewCell來實現,或者使用viewWithTag:(前者更好)。有數百個這樣的例子,所以我不會在這裏重溫。

您也正在調度塊中引用相同的視圖。這也是不正確的,因爲單元可能在塊被調用時被重用。相反,使用indexPath再次獲取單元格,並以此方式進行修改。

最後,您可能需要考慮在後臺線程上舍入圖像角落,而不是使用layer.cornerRadius--這種方法會導致圖像混合,這會在您一次顯示大量圖像時導致動盪不安的動畫。

+0

嗨,謝謝你的回覆。我知道子類,但我不知道如何使用viewWithTag :. – ORStudios