2013-05-16 26 views
1

我正在使用UITableView列出來自Internet的一些圖像。所以我使用AFNetworking'ssetImageWithURLRequest:placeholderImage:success:failure:方法,當圖像下載時,我需要做一些過程然後顯示。爲了刷新處理後的圖像,我使用刷新某些行後,爲什麼表視圖單元格會被搞亂?

[wTableView beginUpdates]; 
[wTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
[wTableView endUpdates]; 

在成功塊。看來這個工程時加載的第一次,但是當我滾動tableview中,該行被搞砸了,還一個行消失,它很容易在[wTableView endUpdates];方法崩潰。這種方法有什麼問題? 相關的代碼片段如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *tableIdentifier = @"cell"; 

    CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier]; 

    if (!cell) { 
     cell = [[CouponTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableIdentifier]; 
    } 
    [cell prepareForReuse]; 

    NSArray *coupons = [mCoupons objectAtIndex:indexPath.section]; 
    CouponDB *couponDB = [coupons objectAtIndex:indexPath.row]; 
    NSString *thumbLink; 
    if (couponDB) { 
     [cell.textLabel setText:couponDB.couponInfo.company]; 
     [cell.textLabel setFont:[UIFont boldSystemFontOfSize:CELL_LABEL_FONT_SIZE]]; 
     [cell.textLabel setTextColor:[UIColor grayColor]]; 
     [cell.textLabel setBackgroundColor:[UIColor clearColor]]; 
     [cell.detailTextLabel setText:[NSString stringWithFormat:@"Expires:%@",couponDB.couponInfo.deadline]]; 
     [cell.detailTextLabel setBackgroundColor:[UIColor clearColor]]; 
     thumbLink = [self thumbLink:couponDB.couponInfo.imgURL]; 
     [cell setNeedsLayout]; 

     if (couponDB.redeem_status) { 
      UIImageView * __weak imageView = cell.imageView; 
      CouponTableController * __weak table = self; 
      UITableView *__weak wTableView = mTableView; 
      [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
       imageView.image = [table crossedImage:image]; 
       @synchronized(wTableView){ 
        [wTableView beginUpdates]; 
        [wTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
        [wTableView endUpdates]; 
       } 

      } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
       NSLog(@"cell imageview load error:%@",error); 
      }]; 
     } else { 
      [cell.imageView setImageWithURL:[NSURL URLWithString:thumbLink] placeholderImage:[UIImage imageNamed:@"default_cover.jpg"]]; 
     } 

    } 

    if (![cell.backgroundView isKindOfClass:[CouponTableCellBackgroud class]]) { 
     cell.backgroundView = [[CouponTableCellBackgroud alloc] init]; 
    } 

    return cell; 
} 

首次正裝表(無滾動)是這樣的: enter image description here

當您滾動向下和向上的錶行搞砸了,這是象下面這樣: enter image description here

任何建議都理解的。

+0

你爲什麼不使用[SDWebImage(https://github.com/rs/SDWebImage)來代替。它使用簡單,並提供緩存。 – Amar

+0

@Amar我使用AFNetworking處理大多數網絡問題,而我認爲AFNetworking就是這個問題就夠了:)。我認爲關鍵問題是圖像下載時,如何通知tableview更新相關單元格。 – chancyWu

回答

0

UIImageView * __weak imageView = cell.imageView; 
CouponTableController * __weak table = self; 
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:[UIImage imageNamed:@"default_cover.jpg"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
       imageView.image = [table crossedImage:image]; 
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
       NSLog(@"cell imageview load error:%@",error); 
    }]; 

只有添加一個佔位符圖像和刪除的tableview更新。那麼它會自動刷新單元格。

0

你可以試試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString *SimpleTableIdentifier = @"YourResuseIdentifier"; 

     CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier]; 

    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier]; 
    } 
    else 
    { 
     for (UIView *subview in cell.contentView.subviews) 
      [subview removeFromSuperview]; 
    } 


    NSArray *coupons = [mCoupons objectAtIndex:indexPath.section]; 
    CouponDB *couponDB = [coupons objectAtIndex:indexPath.row]; 
    NSString *thumbLink; 
    if (couponDB) { 
     [cell.textLabel setText:couponDB.couponInfo.company]; 
     [cell.textLabel setFont:[UIFont boldSystemFontOfSize:CELL_LABEL_FONT_SIZE]]; 
     [cell.textLabel setTextColor:[UIColor grayColor]]; 
     [cell.textLabel setBackgroundColor:[UIColor clearColor]]; 
     [cell.detailTextLabel setText:[NSString stringWithFormat:@"Expires:%@",couponDB.couponInfo.deadline]]; 
     [cell.detailTextLabel setBackgroundColor:[UIColor clearColor]]; 
     thumbLink = [self thumbLink:couponDB.couponInfo.imgURL]; 

     if (couponDB.redeem_status) { 
      UIImageView * __weak imageView = cell.imageView; 
      CouponTableController * __weak table = self; 
      UITableView *__weak wTableView = mTableView; 
      [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
       imageView.image = [table crossedImage:image]; 

       [wTableView beginUpdates]; 
       [wTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
       [wTableView endUpdates]; 


      } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
       NSLog(@"cell imageview load error:%@",error); 
      }]; 
     } else { 
      [cell.imageView setImageWithURL:[NSURL URLWithString:thumbLink] placeholderImage:[UIImage imageNamed:@"default_cover.jpg"]]; 
     } 

    } 

    if (![cell.backgroundView isKindOfClass:[CouponTableCellBackgroud class]]) { 
     cell.backgroundView = [[CouponTableCellBackgroud alloc] init]; 
    } 

    return cell; 
} 
+0

如果(細胞==無) { 細胞= [[ALLOC的UITableViewCell] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier]; } else {for(UIView * subview in cell.contentView.subviews) [subview removeFromSuperview]; } 它不重複使用單元格中的每個刪除視圖,它不重用單元格 – NANNAV

+0

它不起作用:(。 – chancyWu

+0

@NANNAV用於防止重用單元格 – satheeshwaran

0

試試這個,而不是重裝電池。聲明一個弱指針,以我改變負載圖像代碼的單元格

__weak UITableViewCell *weakCell = cell; 
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thumbLink]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
      weakCell.imageView.image = [table crossedImage:image]; 
      [weakCell setNeedsLayout];     

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
      NSLog(@"cell imageview load error:%@",error); 
}]; 
+0

我曾嘗試過,但它不起作用。我已經知道了,無論如何,非常感謝:)。 – chancyWu

相關問題