title: Alternating Row Colors in Xamarin Forms ListView IOS
body: <p>I am trying to write a CustomRenderer that can allow a ListView to have alternating row colors. I have run into an issues and am not sure if it might be a bug or am I just not implementing the renderer correctly:</p>
  
  <p>My expectations for the following code is that each custom view cell should get a color based on it position. However, this is not what is happening, I believe that this this code <code>tv.IndexPathForCell(cell).Row</code> should return the cell's index but it is not working as expected. It is currently returning a null value ( which it should if the cell in not part view), but I believe that the cell is part of the table view and thus my issue. If I comment out the <code>var index = tv.IndexPathForCell(cell).Row;</code> and then set the background color to a static value the code and view works correctly. In addition an exception is thrown because of the null value and the cell is set to the base and the view renders with a default background.</p>
  
  <p>Am I doing something wrong, I have tried several other things but I believe this should be the correct way to do this.     </p>
  
  <p>c# code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[assembly: ExportRenderer(typeof(CustomViewCell), typeof(CustomViewCellRenderer))]
  
  
  namespace AIM.iOS {
  
    class CustomViewCellRenderer : ViewCellRenderer {
      public override UITableViewCell GetCell(Cell item, UITableView tv) {
         UITableViewCell cell;
  
         try {
            cell = base.GetCell(item, tv);
  
            var customCell = (CustomViewCell)item;
            var cell = base.GetCell(item, tv);
  
            var index = tv.IndexPathForCell(cell).Row;
  
            cell.BackgroundColor = (index % 2 == 0) ? Color.Gray.ToUIColor() :
                                                    Color.White.ToUIColor();
         }catch (Exception ex){
            string message = ex.Message;
            cell = base.GetCell(item, tv);
         }
  
         return cell  
      }
    }
  }
  </code></pre>
  
  <p>I am extending the ExtendedViewCell class from Xamarin-Forms-Labs which adds background properties to the ViewCell object. </p>
  
  <pre><code>namespace AIMUI.Controls {
    public class CustomViewCell : ExtendedViewCell {
      public CustomViewCell() { }
    }
  }
  </code></pre>
  
  <p>Here is the Xaml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;localcontrols:ListView&gt;
    &lt;ListView.ItemTemplate&gt;
      &lt;DataTemplate&gt;
        &lt;localcontrols:CustomViewCell&gt;
          &lt;localcontrols:CustomViewCell.View&gt;
          &lt;/localcontrols:CustomViewCell.View&gt;
        &lt;/localcontrols:CustomViewCell&gt;
      &lt;/DataTemplate&gt;
    &lt;/ListView.ItemTemplate&gt;
  &lt;/localcontrols:ListView&gt;
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p>Ok, so I solved the issue with getting the index by using  <code>var index = tv.IndexPathForRowAtPoint(cell.Center).Row;</code>, however this has caused another issue. It works perfectly the first time, as well as coming back to the page from a sub view. However, when I leave the page and go to another view from the <code>masterpage</code> and come back it sets the background of all items gray. Currently, I am creating a new page and then setting it to the <code>masterpage.details</code>, when coming from the menu.   </p>