为了账号安全,请及时绑定邮箱和手机立即绑定

在UITableView单元格中从url加载异步图像-滚动时将图像更改为错误的图像

在UITableView单元格中从url加载异步图像-滚动时将图像更改为错误的图像

千巷猫影 2019-07-06 13:30:49
在UITableView单元格中从url加载异步图像-滚动时将图像更改为错误的图像我编写了两种方法来在我的UITableView单元格中异步加载图片。在这两种情况下,图像将加载良好,但当我滚动表时,图像将更改几次,直到滚动结束,图像将返回到正确的图像。我不知道为什么会这样。#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)- (void)viewDidLoad{    [super viewDidLoad];    dispatch_async(kBgQueue, ^{        NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:                                                       @"http://myurl.com/getMovies.php"]];        [self performSelectorOnMainThread:@selector(fetchedData:)                               withObject:data waitUntilDone:YES];    });}-(void)fetchedData:(NSData *)data{    NSError* error;    myJson = [NSJSONSerialization              JSONObjectWithData:data              options:kNilOptions              error:&error];    [_myTableView reloadData];}    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {    // Return the number of sections.    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // Return the number of rows in the section.    // Usually the number of items in your array (the one that holds your list)    NSLog(@"myJson count: %d",[myJson count]);    return [myJson count];}    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        myCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];        if (cell == nil) {            cell = [[myCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];        }        dispatch_async(kBgQueue, ^{        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://myurl.com/%@.jpg",[[myJson objectAtIndex:indexPath.row] objectForKey:@"movieId"]]]];            dispatch_async(dispatch_get_main_queue(), ^{        cell.poster.image = [UIImage imageWithData:imgData];            });        });         return cell;}
查看完整描述

3 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

/*我已经这样做了,也测试了它*/

步骤1=注册自定义单元格类(对于表中的原型单元格)或对于表的nib(自定义单元格为自定义单元格),如viewDidLoad方法中的如下所示:

[self.yourTableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"CustomCell"];

[self.yourTableView registerNib:[UINib nibWithNibName:@"CustomTableViewCell" bundle:nil] forCellReuseIdentifier:@"CustomCell"];

步骤2=使用UITableView的“deQueeReusableCellWithIdentifier:forIndexPath:”方法(为此,必须注册类或nib):

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];

            cell.imageViewCustom.image = nil; // [UIImage imageNamed:@"default.png"];
            cell.textLabelCustom.text = @"Hello";

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                // retrive image on global queue
                UIImage * img = [UIImage imageWithData:[NSData dataWithContentsOfURL:     [NSURL URLWithString:kImgLink]]];

                dispatch_async(dispatch_get_main_queue(), ^{

                    CustomTableViewCell * cell = (CustomTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
                  // assign cell image on main thread
                    cell.imageViewCustom.image = img;
                });
            });

            return cell;
        }


查看完整回答
反对 回复 2019-07-06
  • 3 回答
  • 0 关注
  • 467 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信