Home > DeveloperSection > Forums > Alternating row colors in UITableView
john rob

Total Post:108

Points:756
Posted on    January-31-2014 11:30 PM

 C# C# 
Ratings:


 1 Reply(s)
 1033  View(s)
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;

        }



Pravesh Singh

Total Post:411

Points:2881
Posted on    January-31-2014 11:43 PM

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


Don't want to miss updates? Please click the below button!

Follow MindStick