ALTERNATING ROW COLORS IN UITABLEVIEW

john rob

Total Post:108

Points:756
Posted by  john rob
C# 
C#
 1480  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. Pravesh Singh

    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

Answer

NEWSLETTER

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