1

テーブル ビュー セルに存在する UIImage に画像を非同期的に読み込もうとしています。正しい画像を表示する前に、別のセルの画像を 1 秒間表示することがあります。

これは私が使用しているコードです

[[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 に答える