我正在使用UITableView
列出来自Internet的一些图像。所以我使用AFNetworking's
setImageWithURLRequest: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;
}
首次正装表(无滚动)是这样的:
当您滚动向下和向上的表行搞砸了,这是象下面这样:
任何建议都理解的。
你为什么不使用[SDWebImage(https://github.com/rs/SDWebImage)来代替。它使用简单,并提供缓存。 – Amar
@Amar我使用AFNetworking处理大多数网络问题,而我认为AFNetworking就是这个问题就够了:)。我认为关键问题是图像下载时,如何通知tableview更新相关单元格。 – chancyWu