UICollectionView duplicating cells?

SMabbuttSMabbutt Member ✭✭

Hey all,

I've been having a problem with the UICollectionView creating duplicate cells and repeating images when I scroll up or down. I realise it's to do with the reusable cells, but without creating my own cell in code, I can't work out how to fix the issue. I'm using a cell on the Storyboard which I'm then using GetCell to populate:

 public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
        {
            var cell = collectionView.DequeueReusableCell(cellIdentifier, indexPath)as CategoryCell;

            string[] job = refList.ToArray();
            string job1 = job[cellCount];
            int rTCount = job.Length;

            if (rTCount > cellCount) {

                var image = UIImage.FromFile("Visuals/" + job1 + " (1).png");

                cell.SetImage(image);

                cellCount++;
                if (rTCount == cellCount)
                {
                    cellCount = 0;
                }
            }

            return cell;

        }

I'm pretty limited on what code I'm at liberty to share as it's a company project. I've tried clearing the cell at the beginning of GetCell in a number of ways, and looked through the settings on the storyboard for a solution, but to no avail.

Any help is appreciated!
Thanks,
Steve

Best Answer

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    UICollectionView will reuse collection view cell in the event GetCell.
    And I've seen you used one condition to set the image:

    if (rTCount > cellCount) 
    {    
        var image = UIImage.FromFile("Visuals/" + job1 + " (1).png");
    
        cell.SetImage(image);
    
        cellCount++;
        if (rTCount == cellCount)
        {
            cellCount = 0;
        }
    }
    

    When rTCount is samller than the cellCount you haven't given the setter. So when rTCount <= cellCount collection view will reuse the cell which is scrolled outside of the screen.
    This is why the same cell shows up again.
    You need to add an else statement to display a different image for your cell under the if statement.

  • SMabbuttSMabbutt Member ✭✭
    edited September 23

    Hmm, i tried this, it seemed like a reasonable response. I tried to just add a simple
    } else {
    cell.SetImage(UIImage.FromFile("logo.gif");
    }
    Just to show up the repeating cells and make sure it was the problem, but the image never appears and the cells continue to repeat. Is the easiest solution to render the cells in code instead of using the storyboard for a template? Or is the problem likely elsewhere?

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited September 23

    What is the cellCount?
    Why not use indexPath.Item directly?
    And try to share your CategoryCell's code.

  • SMabbuttSMabbutt Member ✭✭

    cellCount is simply just an int set to zero at the start of the class, I'm using it to set the cell with the right image from the database results. Without the if statement when i scroll to the bottom of the collectionview the app crashes with "Index was outside the bounds of the array".

    Unsure on using indexPath.Item, I have little experience with Xamarin and it's been years since I've touched a lot of C#, could you explain?

    CategoryCell's code is quite simple and is as follows:

    public partial class CategoryCell : UICollectionViewCell
        {
            public CategoryCell (IntPtr handle) : base (handle)
            {
            }
    
            public void SetImage(UIImage image)
            {
                cellImageView.Image = image;
            }
            public void SetLabel(string label)
            {
                cellLabel.Text = label;
            }
    
        }
    
  • SMabbuttSMabbutt Member ✭✭

    Excellent, thank you! Such a simple thing, i knew I was doing it wrong but just couldn't figure out what I was missing!

Sign In or Register to comment.