2013-05-16 29 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

相关问题