UITableView afnetworking图像交换位置

时间:2013-05-10 16:04:00

标签: objective-c uitableview cell afnetworking

桌面视图和单元格中的图像存在问题。 要加载图像,我使用AFNetworking并使用以下代码:

    [celle.thumb setImageWithURLRequest:[NSURLRequest requestWithURL:MYIMGLINK]
                       placeholderImage:[UIImage imageNamed:@"Placeholder.png"]
                                success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){

                                    if (request) {
                                        //Fade animation
                                        [UIView transitionWithView:celle.thumb
                                                          duration:0.8f
                                                           options:UIViewAnimationOptionTransitionCrossDissolve
                                                        animations:^{
                                                                [celle.thumb setImage:image];



                                                        } completion:NULL];

                                    }


                                }
                                failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){

                                }
     ];

此代码按预期工作,而tableview现在看起来像this

但现在,当我向下滚动,再向上滚动时 - 所有图像都是混合的。他们交换了地方,或者我真的不知道。我不知道如何解决这个问题。 Thats向下滚动后的样子。

我会很高兴你能帮助我。

2 个答案:

答案 0 :(得分:2)

问题是您可能正在使用dequeueReusableCellWithIdentifiercellForRowAtIndexPath方法中创建单元格。这意味着当需要在屏幕上显示新单元格时(如滚动时),以前在视图中的旧单元格将用作新单元格。

只要您在方法cellForRowAtIndexPath中明确设置单元格中的所有内容,这不是问题。看起来您正在设置placeholderImage,但这可能无法正常工作。我可能会在执行setImageWithURLRequest之前设置图像行。

答案 1 :(得分:0)

我遇到了类似的问题 - 它与iOS重用现有单元格有关。因此,您需要做的就是将其设置为nil,如下所示:

celle.thumb.image = nil;

您的代码块现在看起来应该是这样的:

celle.thumb.image = nil;

[celle.thumb setImageWithURLRequest:[NSURLRequest requestWithURL:MYIMGLINK]
                       placeholderImage:[UIImage imageNamed:@"Placeholder.png"]
                                success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){

                                if (request) {
                                    //Fade animation
                                    [UIView transitionWithView:celle.thumb
                                                      duration:0.8f
                                                       options:UIViewAnimationOptionTransitionCrossDissolve
                                                    animations:^{
                                                            [celle.thumb setImage:image];



                                                    } completion:NULL];

                                }


                            }
                            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){

                            }
 ];
相关问题