如何使用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;
}
答案 0 :(得分: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;
}
}
}
在你的tableview中使用它:
var tvdelegate = new AlternatingTableViewDelegate()
myTableView .Delegate = tvdelegate
答案 1 :(得分:1)
有一种更好的方法可以做到这一点。您需要做的就是在GetCell方法中设置背景颜色,交替使用线条颜色,如下所示。它还解决了在tableview中不处理线上触摸的问题。
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {
if (indexPath.Row % 2 == 0) {
ContentView.BackgroundColor = UIColor.DarkGray;
} else {
ContentView.BackgroundColor = UIColor.DarkTextColor;
}
}