2

モノタッチを使用して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;

        }
4

2 に答える 2

7

このクラスを追加します。

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;
        }
    }
}

テーブルビューで使用します。

var tvdelegate = new AlternatingTableViewDelegate()
myTableView .Delegate = tvdelegate
于 2012-12-04T14:30:32.573 に答える
1

これを行うためのより良い方法があります。ここに示すように、GetCell メソッドで背景色を線の色と交互に設定するだけです。また、テーブルビューの行でタッチを処理しないという問題も解決します。

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
        if (indexPath.Row % 2 == 0) {
                ContentView.BackgroundColor = UIColor.DarkGray;
        } else {
                ContentView.BackgroundColor = UIColor.DarkTextColor;
        }
}
于 2016-01-14T17:44:53.910 に答える