Alternating row colors in collection view.

JPHochbaumJPHochbaum USMember ✭✭✭
edited December 2016 in Xamarin.iOS

I was easily able to do this in the array adapter of an android app, but my iOS app I am having trouble getting this to work.

    public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
    {
        var cell = (CustomCollectionViewCell)collectionView.DequeueReusableCell(CustomCollectionViewCell.cellID, indexPath);
        if (indexPath.Row % 2 != 0)
        {
            var x = 0;
            cell.ContentView.BackgroundColor = UIColor.White;
            cell.BackgroundColor = cell.ContentView.BackgroundColor;

        }

        cell.UpdateCell(rows[indexPath.Row]);
        return cell;
    }

As you can see when my row number is an odd one it switches color to white for the cells. Right now they are all remaining gray.

Best Answer

Answers

  • prashantvcprashantvc USXamarin Team Xamurai

    @JPHochbaum there is very detailed answer on stackflow for similar question here: http://stackoverflow.com/a/18324146/85606

    The code is in ObjC but it can easily be ported to C#, let me know if you need further help

  • JPHochbaumJPHochbaum USMember ✭✭✭

    @prashantvc said:
    @JPHochbaum there is very detailed answer on stackflow for similar question here: http://stackoverflow.com/a/18324146/85606

    The code is in ObjC but it can easily be ported to C#, let me know if you need further help

    The issue isn't that I don't know how to alternate the colors. My current code will do just that. My wording in this was probably poorly written.

    What I need to know is why the background color in the cells are not changing color. In debug mode every other row is going into my if condition so that alternating color change is being caught, but the colors are not taking effect.

    It only takes effect if I were to go into the main.storyboard and change the color scheme there, programatically it isn't sticking.

  • JPHochbaumJPHochbaum USMember ✭✭✭

    Nevermind my code worked this morning! Very weird!

Sign In or Register to comment.