刷新一些行后为什么表视图单元格搞乱了?

时间:2013-05-16 03:43:05

标签: ios uitableview uiview afnetworking

我正在使用UITableView列出一些来自互联网的图片。所以我使用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;
}

第一次加载表(没有滚动)是这样的: enter image description here

当你向下滚动时,表格的行被弄乱了,如下所示: enter image description here

任何建议都表示赞赏。

3 个答案:

答案 0 :(得分: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);
 }];

答案 1 :(得分: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;
}

答案 2 :(得分: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更新。然后它会自动刷新单元格。