2

I used SDWEBImage library to show images in scrollview under the tableview.

My problem is, images load completely but when I scroll tableview flashy images from previous cells or duplicate images show in Imageview. I programmatically created an imageview like below:

enter image description here

My code is below:

for (NSString *stringurl in [[[Arr objectAtIndex:indexPath.row]valueForKey:@"sports"]valueForKey:@"image"]) {

            UIImageView *yourImageView =[[UIImageView alloc] initWithFrame:CGRectMake(x,10,40,40)];

            NSURL *urlimg=[NSURL URLWithString:stringurl];

           [yourImageView sd_setImageWithURL:urlimg
                                placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row ? SDWebImageRefreshCached : 0];
      [cell.arenaimgscroll addSubview:yourImageView];

            x = x + 50;
            if (stringurl==NULL) {
                cell.arenaimgscroll.hidden=YES;
            }
            else{
                cell.arenaimgscroll.hidden=NO;

            }
        }
NSNoob
  • 5,548
  • 6
  • 41
  • 54
Psnt143
  • 55
  • 8
  • Don't add screenshots. Show code. – NSNoob Aug 04 '16 at 07:57
  • programmetically added imageview in this tableviewcell .in this cell i used scrollview to show multiple images in one cell.data passed in webservice. – Psnt143 Aug 04 '16 at 08:09
  • You can also use `UICollectionView` inside `UITableViewCell`, refer this [git repository](https://github.com/DipenPanchasara/UITableViewWithHorizontalScroll). – Dipen Panchasara Aug 04 '16 at 10:10

1 Answers1

0

Just a short overview, So you get your answer

UITableView is highly optimized, and thus only keep On-screen visible rows in memory. Now, All rows Cells are cached in Pool and are reused and not regenerated. Whenever, user scrolls the UITableView, it adds the just-hidden rows in Pool and reuses them for next to be visible rows.

So, now, coming to your answer

When you scroll your UITableView, UITableView datasource method gets called again for every indexPath, thus dequeueReusableCellWithIdentifier gives you cached cell, which already have UIImageView added, but you add UIImageView again to it(SOURCE OF ERROR)

Better solution will be to add imageView in interface builder

or

you can check, if imageView already added and then skip the step of adding it again

UPDATE:

NOTE : This solution is not optimized as you are already not reusing image elements, but will resolve your problem.

Add this script above your for loop, this is for clearing older images(that got reused) from your scrollview(arenaimgscroll) :

    [cell.arenaimgscroll.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];

    for (NSString *stringurl in [[[Arr objectAtIndex:indexPath.row]valueForKey:@"sports"]valueForKey:@"image"]) {
   //YOUR CODE
}
gunjot singh
  • 2,578
  • 20
  • 28
  • oh, that is for removing the subviews from arenaimgscroll. By this we make sure that older images that got reused from previous cell are removed from scrollview. – gunjot singh Sep 10 '16 at 06:46
  • Sorry, forgot - you are using Objective C and not Swift! Updating my answer. – gunjot singh Sep 10 '16 at 06:49