H3rrVorr4g3nd
H3rrVorr4g3nd

Reputation: 265

UITableView afnetworking images swap positions

I have a problem with a tableview and images in the cells. To load the images I use AFNetworking with the following code:

    [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){

                                }
     ];

This code does work as expected and the tableview now looks like this

But now, when I scroll down, and up again - all images are mixed. They swapped places or I dont really know. And I don't have an idea how to solve this problem. Thats what it looks like after scrolling down and up.

Would be so happy I you could help me.

Upvotes: 1

Views: 693

Answers (2)

Yas Tabasam
Yas Tabasam

Reputation: 10615

I had a similar issue - it is to do with iOS reusing your existing cells. Therefore all you need to do is set it to nil as following:

celle.thumb.image = nil;

Your code block should now look something like this:

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){

                            }
 ];

Upvotes: 0

ZappyCode
ZappyCode

Reputation: 75

The issue is you are probably using dequeueReusableCellWithIdentifier to create your cells in the cellForRowAtIndexPath method. The means that when a new cell needs to be shown on screen (like when you scroll) that an old cell that was previously in view will be used as the new one.

This isn't an issue as long as you explicitly set everything in the cell in the method cellForRowAtIndexPath. It looks like you are setting placeholderImage but that might not be working. I would maybe set the image line before you do the setImageWithURLRequest.

Upvotes: 2

Related Questions