C# iOS Xamarin tableview 复用的使用

来源:互联网 发布:z字寻路算法 编辑:程序博客网 时间:2024/06/08 20:02


        我们大概了解了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