C# UITableView中的交替行颜色

C# UITableView中的交替行颜色,c#,uitableview,xamarin.ios,C#,Uitableview,Xamarin.ios,如何使用monotouch在UITableView中获得交替行颜色 我目前正在使用以下方法填充我的TableView: public partial class myViewController : UITableViewController { public static UINavigationController navigationController; public static IntPtr handle; public Co

如何使用monotouch在UITableView中获得交替行颜色

我目前正在使用以下方法填充我的TableView:

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;

        }
公共部分类myViewController:UITableViewController
{
公共静态UINavigationController导航控制器;
公共静态IntPtr句柄;
上市公司视图控制器(IntPtr句柄):基本(句柄)
{
}
公共覆盖无效ViewDidLoad()
{
base.ViewDidLoad();
服务=新服务();
var myList=service.GetList(param1);
列表=新列表();
foreach(myList中的变量c)
{
列表。添加(c.名称);
}
navigationController=navigationController;
把手=把手;
var source=新公司TableSource(列表);
myTableView.Source=Source;
}
添加此类:

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;
        }
    }
}
在tableview中使用它:

var tvdelegate = new AlternatingTableViewDelegate()
myTableView .Delegate = tvdelegate

有一种更好的方法可以做到这一点。您所需要做的就是在GetCell方法中设置背景色,交替线条颜色,如图所示。它还解决了在tableview中不处理线条上的触摸的问题

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
        if (indexPath.Row % 2 == 0) {
                ContentView.BackgroundColor = UIColor.DarkGray;
        } else {
                ContentView.BackgroundColor = UIColor.DarkTextColor;
        }
}

在哪种情况下应该分配代理?在viewdidload中,您还可以分配数据源。UITableView用于向下钻取到所接触的行。添加此项似乎会导致UITableView不允许导航到下一个视图。不允许添加base.willAppeal。我缺少什么?如果没有s,我无法准确地说查看所涉及的完整代码。tableview上可能已经有另一个UITableViewDelegate用于“深入”行为。您可以将此WillDisplay函数添加到该UITableViewDelegate。但无法确定,因为此代码不显示您的“深入”行为来自何处以及另一个委托是否为l已在使用中