1

我正在尝试在位于表格视图单元格中的 UIImage 中异步加载图像。有时它会在显示正确的图像之前显示来自另一个单元格的图像一秒钟。

这是我正在使用的代码

[[TMCache sharedCache] objectForKey:post[@"gif"] block:^(TMCache *cache, NSString *key, id object) {
                if (object) {
                    if ([self isRowZeroVisible:indexPath.section]) {
                        [GIFLoader loadGIFData:object to:postGif for:feedTableView];
                    }
                    return;
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    __block NSURL* url = [NSURL URLWithString:post[@"gif"]];
                    NSURLRequest* req = [NSURLRequest requestWithURL:url];
                    
                    OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
                    [loader startRequestWithResponseHandler:nil
                    progress:nil
                    completion:^(NSData* receivedData, NSInteger httpStatusCode) {
                        if ([self isRowZeroVisible:indexPath.section]) {
                            
                            [GIFLoader loadGIFData:receivedData to:postGif for:feedTableView];
                        }
                        [[TMCache sharedCache] setObject:receivedData forKey:post[@"gif"]];
                    } errorHandler:nil];
                });

            }];


[GIFLoader loadGIFData:receivedData to:postGif for:feedTableView]; is a method I created that loads GIFs in a uiimageview. it does that in a background thread and then assigns the Image in the main thread though.

对于异步加载,我需要遵循某些做法吗?

我不确定我的代码有什么问题。任何帮助将不胜感激

编辑

//Loading stuff into tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @autoreleasepool {
        
        //Type of Cells
        static NSString *simpleTableImageIdentifier = @"gifCell";
        static NSString *simpleTableBodyIdentifier = @"bodyCell";
        static NSString *simpleTableActionsIdentifier = @"actionsCell";
        UITableViewCell *cell = nil;
        
        //Data that goes in the cell
        NSDictionary *post = [dataArray objectAtIndex:indexPath.section];
        
        //User info
        UIImageView *userAvatar;
        UILabel *postUserFullNameLabel;
        UILabel *postUsername;
        
        //GIF Date
        OHAttributedLabel* gifDate = nil;
        NSMutableAttributedString* gifDateString;
        
        //Feed GIF
        NSString *gifBody = [[NSString alloc]init];
        OHAttributedLabel* attrLabel = nil;
        NSMutableAttributedString* mas;
        
        //Location
        OHAttributedLabel* gifLocation = nil;
        NSMutableAttributedString* gifLocationString;
        UILabel *locationBg;
        
        //Buttons
        UIButton *btn1 =[[UIButton alloc] initWithFrame:CGRectMake(11,0,50,30)];
        UIButton *btn2 =[[UIButton alloc] initWithFrame:CGRectMake(69,0,50,30)];
        UIButton *btn3 =[[UIButton alloc] initWithFrame:CGRectMake(259,0,50,30)];
        
        if (indexPath.row == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:simpleTableImageIdentifier];
            
            if (cell == nil){
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableImageIdentifier];
            }
            
            __block UIImageView *postGif= (UIImageView *)[cell viewWithTag:104];
            
            postGif.image = nil;
            
            [[TMCache sharedCache] objectForKey:post[@"gif"] block:^(TMCache *cache, NSString *key, id object) {
                if (object) {
                    if ([self isRowZeroVisible:indexPath.section]) {
                        [GIFLoader loadGIFData:object to:postGif for:feedTableView];
                    }
                    return;
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    __block NSURL* url = [NSURL URLWithString:post[@"gif"]];
                    NSURLRequest* req = [NSURLRequest requestWithURL:url];
                    
                    OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
                    [loader startRequestWithResponseHandler:nil
                    progress:nil
                    completion:^(NSData* receivedData, NSInteger httpStatusCode) {
                        if ([self isRowZeroVisible:indexPath.section]) {
                            
                            [GIFLoader loadGIFData:receivedData to:postGif for:feedTableView];
                        }
                        [[TMCache sharedCache] setObject:receivedData forKey:post[@"gif"]];
                    } errorHandler:nil];
                });

            }];
            
            postGif.layer.cornerRadius = 2.0;
            postGif.layer.masksToBounds = YES;
            postGif.clipsToBounds = YES;
            
            
            locationBg = (UILabel *)[cell viewWithTag:106];
            
            
            userAvatar = (UIImageView *)[cell viewWithTag:100];
            if ([post [@"user_avatar"] length ] > 0) {
                NSString *img = [@"https://d1f7i732a4e7fw.cloudfront.net/" stringByAppendingString:post [@"user_avatar"]];
                [userAvatar setImageWithURL:[NSURL URLWithString:img] placeholderImage:[UIImage imageNamed:@"userDefaultPicture.png"]];
            }else{
                userAvatar.image = [UIImage imageNamed:@"userDefaultPicture.png"];
            }
            userAvatar.layer.cornerRadius = 18.0;
            userAvatar.layer.borderWidth = 1.0;
            userAvatar.layer.borderColor = (__bridge CGColorRef)([UIColor lightGrayColor]);
            userAvatar.clipsToBounds = YES;
            
            
            postUserFullNameLabel = (UILabel *)[cell viewWithTag:101];
            postUserFullNameLabel.text = post[@"user_fullname"];
            
            
            
            postUsername = (UILabel *)[cell viewWithTag:102];
            postUsername.text = [NSString stringWithFormat:@"@%@",post[@"user_username"]];
        
            
            gifDate = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(246,9,60,21)];
            gifDate.autoresizingMask = UIViewAutoresizingNone;
            gifDate.centerVertically = YES;
            gifDate.highlightedTextColor = [UIColor whiteColor];
            gifDate.tag = 103;
            gifDate.backgroundColor = [UIColor clearColor];
            gifDate.extendBottomToFit = NO;
            
            
            gifDateString = [NSMutableAttributedString attributedStringWithString:[NSString stringWithFormat:@"\ue003 %@",post[@"date"]]];
            [gifDateString setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0]];
            [gifDateString setFont:[UIFont fontWithName:@"icomoon" size:10.0] range:NSMakeRange(0,1)];
            [gifDateString setTextColor:[UIColor whiteColor]];
            [gifDateString setTextAlignment:kCTTextAlignmentRight lineBreakMode:kCTLineBreakByTruncatingTail];
            [OHASBasicMarkupParser processMarkupInAttributedString:gifDateString];
            gifDate.attributedText = gifDateString;
            
            if (!(post[@"latitude"] == (id)[NSNull null])){
                //Location of the Post
                gifLocation = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(165,27,141,21)];
                gifLocation.autoresizingMask = UIViewAutoresizingNone;
                gifLocation.centerVertically = YES;
                gifLocation.highlightedTextColor = [UIColor whiteColor];
                gifLocation.tag = 107;
                gifLocation.backgroundColor = [UIColor clearColor];
                gifLocation.extendBottomToFit = NO;
                
                gifLocationString = [NSMutableAttributedString attributedStringWithString:[NSString stringWithFormat:@"\uf041 %@",post[@"locationName"]]];
                [gifLocationString setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0]];
                [gifLocationString setFont:[UIFont fontWithName:@"icomoon" size:12.0] range:NSMakeRange(0,1)];
                [gifLocationString setTextColor:[UIColor whiteColor]];
                [gifLocationString setTextAlignment:kCTTextAlignmentRight lineBreakMode:kCTLineBreakByTruncatingTail];
                [OHASBasicMarkupParser processMarkupInAttributedString:gifLocationString];
                gifLocation.attributedText = gifLocationString;
            }
        }
        
    else if (indexPath.row == 1) {
        cell = [tableView dequeueReusableCellWithIdentifier:simpleTableBodyIdentifier];
        
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableBodyIdentifier];
        }
        
        //Body of GIF
        // Add more from here http://www.easyapns.com/iphone-emoji-alerts
        gifBody = post[@"body"];
        
        attrLabel = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(10,5,kLabelWidth,tableView.rowHeight-2*kLabelVMargin)];
        attrLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        attrLabel.centerVertically = NO;
        attrLabel.automaticallyAddLinksForType = NSTextCheckingAllTypes;
        attrLabel.delegate = self;
        attrLabel.highlightedTextColor = UIColorFromRGB(0x333333);
        attrLabel.tag = kAttributedLabelTag;
        attrLabel.backgroundColor = [UIColor clearColor];
        attrLabel.extendBottomToFit = YES;
        [cell.contentView addSubview:attrLabel];
        
        attrLabel = (OHAttributedLabel*)[cell viewWithTag:kAttributedLabelTag];
        mas = [NSMutableAttributedString attributedStringWithString:gifBody];
        [mas setFont:[UIFont fontWithName:@"Helvetica Neue" size:14.0]];
        [mas setTextColor:UIColorFromRGB(0x333333)];
        [mas setTextAlignment:kCTTextAlignmentLeft lineBreakMode:kCTLineBreakByWordWrapping];
        [OHASBasicMarkupParser processMarkupInAttributedString:mas];
        attrLabel.attributedText = mas;
    }
        
    else if (indexPath.row == 2) {
        cell = [tableView dequeueReusableCellWithIdentifier:simpleTableActionsIdentifier];
        
        if (cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableActionsIdentifier];
        }
        
        [self customizeButtons:btn1];
        btn1.tag = (indexPath.section +1)*200;
        btn1.titleLabel.font = [UIFont fontWithName:@"icomoon" size:16.0];
        if ([post[@"is_Favoring"] boolValue]) {
            [btn1 setTitleColor:UIColorFromRGB(0xE4717A) forState:UIControlStateNormal];
        }else{
            [btn1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
        [btn1 setTitle:@"\uf004" forState:UIControlStateNormal];
        btn1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [btn1 addTarget:self
                            action:@selector(favorAction:)
                  forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn1];
        
        
        [self customizeButtons:btn2];
        btn2.tag = (indexPath.section +1)*2000;
        btn2.titleLabel.font = [UIFont fontWithName:@"icomoon" size:16.0];
        [btn2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn2 setTitle:@"\ue000" forState:UIControlStateNormal];
        [cell.contentView addSubview:btn2];
        btn2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [btn2 addTarget:self
                 action:@selector(commentAction:)
       forControlEvents:UIControlEventTouchUpInside];
        
        
        [self customizeButtons:btn3];
        btn3.titleLabel.font = [UIFont fontWithName:@"icomoon" size:16.0];
        [btn3 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn3 setTitle:@"\ue001" forState:UIControlStateNormal];
        btn3.tag = (indexPath.section +1)*20000;
        [cell.contentView addSubview:btn3];
        btn3.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        [btn3 addTarget:self
                 action:@selector(otherAction:)
       forControlEvents:UIControlEventTouchUpInside];
    }
        
        //Paginiation
        if (indexPath.section == [dataArray count]-4 && indexPath.row == 1) {
            [self loadNextPage];
        }
    
        return cell;
    }
}
4

2 回答 2

2

您看到来自另一个单元格的图像的原因是为了提高效率,表格单元格被重用。要解决此问题,请将您的图像设置为nil在您将可重用单元格出队后在tableView:cellForRowAtIndexPath:.

于 2013-05-13T21:00:13.320 回答
2

首先,您不应该在 cellForRowAtIndexPath 中做所有这些事情。

您应该继承 UITableViewCell 并将单元格布局在它自己的类中。

indexPath 处的行单元格应加载单元格并填充数据。不是控件的布局负载。

其次,完成此操作后,您可以致电...

- (void)prepareForReuse
{
    self.imageView.image = nil;
}

这将在单元格的出列时清除 imageView,这样如果加载图像有延迟,那么您只会看到一个空白的 imageView,而不是之前在那里的图像。

于 2013-05-13T22:23:50.070 回答