TableView UITableViewSource 使用详解

mac2022-06-30  89

TableView UITableViewSource 使用详解 IOS中的 TableView 和android中的listview 类似,都为数据列表类的控件。  在使用 tableView 的时候,tableview 必须指定TableSource.所以我们需要学习UITableViewSource  tableview 中的每个Cell由两部分组成,一个header。一个cell。 如果需要使用header则需要在该tableView 数据结构为 list<entity,list<entity>>.为了方面期间本例使用的entity结构如下  所以 tableSource 中的data 则为 List<entityGroup> EntityItem {  …… } EntityGroup { list<entityItem>   Items, …… } 如果为单一Cell 则数据结构list<entity> 该例子介绍 list<entity,list<entity>> 结构数据的加载, 如果 单 list<entity> 可直接忽略 关于header处理的代码即可 需要自定义 UITableViewSource ,如下: public class CustomTableViewSource :UITableViewSource { List<EntityGroup> lsdata; public CustomTableViewSource(List<EntityGroup> data) { lsdata = data; } public override int NumberOfSections (UITableView tableView)
 {
 return lsdata.Count;
 }

 public override int RowsInSection (UITableView tableview, int section)
 {
 return lsdata[section].Items.Count;
 } /// 该方法主要 构造 Header 其中使用的 TabHead 为 自定义的cell。 如何自定义cell或view(xib)详见xamarin系列博客 public override UIView GetViewForHeader (UITableView tableView, int section)
 { //加载自定义的xib 替换系统提供的header 实现自定义Header 样式
 TabHead head = new TabHead ();
 var views = NSBundle.MainBundle.LoadNib ("TabHead", head, null);
 head = MonoTouch.ObjCRuntime.Runtime.GetNSObject (views.ValueAt (0)) as ServiceCostTabHead;
 head.SetData (lsdata[section]);
 return head;
 } /// 该方法主要 构造 Cell 其中使用的 ItemCell 为 自定义的cell。 如何自定义cell或view(xib)详见xamarin系列博客 public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
 {
 //加载自定义的xib 替换系统提供的Cell 实现自定义Cell样式
 customtablecell cell = (customtablecell)tableView.DequeueReusableCell(“自定义标示”);
 if (cell==null) 
 {
 EntityItem item = lsdata[indexPath.Section].Items [indexPath.Row];
 item = _serviceCostGroup [indexPath.Section].Items [indexPath.Row];
 cell = new ItemCell ();
 var views = NSBundle.MainBundle.LoadNib ("ItemCell", cell, null);
 cell = MonoTouch.ObjCRuntime.Runtime.GetNSObject (views.ValueAt (0)) as ItemCell;
 cell.SetData (item);
 }
 return cell;

 } /// 如果自定义的cell高度不一致,可在此设置 cell的高度,根据实际条件判断返回 float 类型数据。 public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
 { //if else return 100f; } /// 如果自定义的Header高度不一致,可在此设置 Header的高度,根据实际条件判断返回 float 类型数据。
public override float GetHeightForHeader (UITableView tableView, int section)
 {//if else
 return 46f;
 }
 } 现在 tablesource 的准备工作就绪。 只需要tableview 设置 CustomTableViewSource tablesource = new CustomTableViewSource(data); tableview.Source = tablesource; 关注 xamarin.ios 系列博客,介绍关于自定xib, tableview 的行选中事件,和 一个超简单实现上啦加载更多数据。 


转载于:https://www.cnblogs.com/mendeliang/p/5070852.html

最新回复(0)