0

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;
}

第一次加载表格(无滚动)是这样的: 在此处输入图像描述

当您向下和向上滚动时,表格行被弄乱了,如下所示: 在此处输入图像描述

任何建议表示赞赏。

4

3 回答 3

1

试试这个,而不是重新加载单元格。声明一个指向单元格的弱指针

__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);
 }];
于 2013-05-16T07:26:05.207 回答
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;
}
于 2013-05-16T04:05:53.793 回答
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 更新。然后它会自动刷新单元格。

于 2013-05-16T07:43:19.247 回答