Alternating row colors in UITableView

Total Post:108

Points:756
C# 
C#
 1752  View(s)
Ratings:
Rate this:

How do I get alternating row colors in a UITableView using monotouch?

I am currently populating my TableView using this:

public partial class myViewController : UITableViewController
    {
        public static UINavigationController navigationController;
        public static IntPtr handle;
        public CompanyViewController (IntPtr handle) : base (handle)
        {
        }
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            Services service = new Services();
            var myList = service.GetList(param1);
            List<string> list = new List<string>();
            foreach(var c in myList)
            {
                list.Add (c.Name);
            }
            navigationController = NavigationController;
            handle = Handle;
            var source = new CompanyTableSource(list);
            myTableView.Source = source;
        }
  1. Post:412

    Points:2888
    Re: Alternating row colors in UITableView

    Hi John,


    Add this class:

    public class AlternatingTableViewDelegate :UITableViewDelegate
    {
        public override void WillDisplay (UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
        {
            if(indexPath.Row % 2 == 0) {
                cell.BackgroundColor = UIColor.White;
            } else {
                cell.BackgroundColor = UIColor.LightGray;
            }
        }
    }

    Use it in you tableview:

    var tvdelegate = new AlternatingTableViewDelegate()

    myTableView .Delegate = tvdelegate

      Modified On Apr-04-2018 03:47:20 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!