在表格中滚动时,单元格会更改位置

时间:2012-03-26 11:23:01

标签: objective-c uitableview scroll cells

#import "UIImageView+AFNetworking.h"

当我滚动我的表格时。细胞改变了位置。这就像他不知道第一个细胞是什么样的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    if(indexPath.section == 0 )
    {
        Recipe *dish = [_recipes objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.user;
        cell.imageView.image = _imageView.image;
        return cell;
    }
    else if (indexPath.section == 1 || indexPath.section == 2)
    {
        Recipe *dish = [_recipesExtra objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.title;
        [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: dish.url]] 
                              placeholderImage:nil 
                              success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                           [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
                                       } 
                              failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                       }];
        return cell;
    }
    else
    {
        return nil;
    }


}

我确定我的错误在reloadRowsAtIndexPaths中,但我不知道如何修复它。

1 个答案:

答案 0 :(得分:1)

我创建了一个新属性,我可以保存图像,因此当重新加载时,他只需拍摄“保存”的图像,这样他就不需要从互联网上加载图像了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    if(indexPath.section == 0 )
    {
        Recipe *dish = [_recipes objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.user;
        cell.imageView.image = _imageView.image;
        return cell;
    }
    else if (indexPath.section == 1 || indexPath.section == 2)
    {
        Recipe *dish = [_recipesExtra objectAtIndex:indexPath.row];
        cell.textLabel.text = dish.title;
        if (dish.image) {
            cell.imageView.image = dish.image;
        } 
        else 
        {
            [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: dish.url]] 
                                  placeholderImage:nil 
                                           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                               dish.image = image;
                                               [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
                                           } 
                                           failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                           }];

        }

        return cell;
    }
    else
    {
        return nil;
    }
}