C# iOS Xamarin tableview 复用的使用


        我们大概了解了tableview的复用机理,我们接着上上一片的文章,来实现一级二级数据的现实,这里不仅仅是cell用到了复用问题,而且还将section进行了复用,道理其实还是一样的,不得不说苹果的那一群专家们可不像中国的专家,他们在这方面可是花费了不少的心血。

       首先,我们应该考虑一个一级二级的数据应该如何传递,最简单的,我们将一级数据作为一个数据源,二级数据作为一个数据源,但是随着我们以后要对改数据进行操作,两个数据源可能会带给我们逻辑上面的混淆;所以在这里,我们采取了一个数据源,就是将一级数据和二级数据写到一起,那么我们就先建一个Data类,里面一个用来存一级标题为string类型的Title,二级标题为list类型的SecondTitle;如下面所示:

using System;
using Foundation;
using System.Collections.Generic;

namespace Expland
{
	public class Data
	{
		public Data ()
		{
		}
		public  string Title{ get; set;}
		public  List<string> SecondTitle = new List<string> ();
	}
}

      那么,我们在页面加载的时候,定一个list<Data>类型的Title,并且给它附上值,然后将Title传递到tableviewSource中去。

using System;
using Foundation;
using System.Collections.Generic;
using UIKit;

namespace Expland
{
	public partial class ViewController : UIViewController
	{
		UITableView tableview;
		List<Data> Titile;

		public ViewController (IntPtr handle) : base (handle)
		{ }

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			Titile = new List<Data> () {
				new Data{Title="A",SecondTitle=new List<string>{"1","2","3","4"}},
				new Data{Title="B",SecondTitle=new List<string>{"1","2","3","4"}},
				new Data{Title="C",SecondTitle=new List<string>{"1","2","3","4"}},
				new Data{Title="D",SecondTitle=new List<string>{"1","2","3","4"}},
				new Data{Title="E",SecondTitle=new List<string>{"1","2","3","4"}},
				new Data{Title="F",SecondTitle=new List<string>{"1","2","3","4"}},

			};
			InitTableView ();
			InitNavigation ();
			//tableview的数据源
			this.tableview.Source = new MyTableViewSource (Titile);

		}
		//tableview初始化
		void InitTableView ()
		{
			tableview = new UITableView (new CoreGraphics.CGRect (0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height));
			this.View.AddSubview(tableview);
		}

		//导航栏初始化
		void InitNavigation()
		{
			this.NavigationItem.Title = "首页";
		}
		public override void DidReceiveMemoryWarning ()
		{
			base.DidReceiveMemoryWarning ();
		}
	}
}


      在 MyTableViewSource类中,我们是通过数据源Title进行操作的,比如说,每一个section的行数,我们这里返回的就是相应位置的二级标题的个数;其他的同理,代码如下:

using System;
using UIKit;
using Foundation;
using System.Collections.Generic;

namespace Expland
{
	public class MyTableViewSource:UITableViewSource
	{
		string cellReuseId="cellReuseId";
		string headerReuseId="headerReuseId";
		List<Data> Title;
		//构造函数,传递参数
		public MyTableViewSource (List<Data> Title)
		{
			this.Title = Title;
		}
		//每一行的内容
		public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
		{
			UITableViewCell cell = tableView.DequeueReusableCell (cellReuseId);
			if (cell == null) {
				cell = new UITableViewCell (UITableViewCellStyle.Default, cellReuseId);
			}
			cell.TextLabel.Text = Title [(int)indexPath.Section].SecondTitle [(int)indexPath.Row];
			return cell;
		}
		//每一个section的行数
		public override nint RowsInSection (UITableView tableview, nint section)
		{
			return Title[(int)section].SecondTitle.Count;
		}
		//section的个数
		public override nint NumberOfSections (UITableView tableView)
		{
			return Title.Count;
		}
		//section的高度
		public override nfloat GetHeightForHeader (UITableView tableView, nint section)
		{
			return 44f;
		}
		//section的内容
		public override UIView GetViewForHeader (UITableView tableView, nint section)
		{
			UITableViewHeaderFooterView headerView = tableView.DequeueReusableHeaderFooterView (headerReuseId);
			if (headerView == null) {
				headerView = new UITableViewHeaderFooterView ();
			}
			headerView.TextLabel.Text=Title[(int) section].Title;

			return headerView;
		}
	}
}




     我们还可以加入缩进,每一个cell都比section的位置靠里,大小是可以控制的,加入以下的代码:

//cell的缩进
public override nint IndentationLevel (UITableView tableView, NSIndexPath indexPath)
{
     return 2;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值