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 ();}}}
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
- C# iOS Xamarin tableview 复用的使用
- C# iOS Xamarin tableview
- ios tableView 的使用
- iOS--TableView的简单使用
- iOS TableView的基本使用
- ios的tableView使用详解
- iOS探索--TableView的使用
- Xamarin体验:使用C#开发iOS/Android应用
- Xamarin体验:使用C#开发iOS/Android应用
- Xamarin体验:使用C#开发iOS/Android应用
- Xamarin 从原生IOS Native Library SDK创建Xamarin版本的C# SDK Bindings
- Xamarin的环境搭建Xamarin.iOS
- iOS tableView的Cell的使用方式
- iOS 使用两个tableview的瀑布流
- iOS开发笔记--TableView的详细使用
- iOS 中tableview的使用
- 【IOS】 TableView and sectionIndex 的使用
- iOS中tableview的使用(1)
- Android Mac下安装Apk到模拟器上
- Ubuntu14.0.4 Tomcat+MySQL+Java CLI配置
- 如何查看已经安装的python软件包和版本
- 路径中有空格导致无法访问资源
- php 文件上传
- C# iOS Xamarin tableview 复用的使用
- 从底层谈WebGIS 原理设计与实现(九):WebGIS中的矢量查询(针对AGS和GeoServer)
- IOS 学习笔记
- Eclipse怎样导入格式化模板
- LeetCode - 189. Rotate Array
- JQueryiframe页面操作父页面中的元素与方法(实例讲解)
- Facebook的人工智能要靠这个人:Yann LeCun
- map bean转换 反射 泛型
- iOS 亮度传感器 获取屏幕亮度值