读了一位园友写的使用MonoTouch.Dialog简化iOS界面开发,我来做个补充;

相信使用过DialogViewController(以下简称DVC)的同学都知道它的强大,但是缺点也是明显的,应付不来复杂的UI布局;

因为DVC的View就是一个UITableView;有的时候不得不放弃DVC来满足复杂UI的要求;

但是用过了DVC后,回过头来使用原生的UITableView是一件很痛苦的事;

我们想要的只是UITableView能够基于RootElement的来进行UI构造,所以只要把这部分功能从DVC择出来就行了。

首先从UITableView派生TableView

public class TableView : UITableView
{
private RootElement root;
public RootElement Root
{
get
{
return root;
}
set
{
if (root == value)
return;
root = value;
root.TableView = this;
root.Prepare();
Source = root.UnevenRows ? new SizingSource(root) : new Source(root);
}
} public TableView()
{
InitializeComponent();
} public TableView(UITableViewStyle style)
: base(RectangleF.Empty, style)
{
InitializeComponent();
} protected virtual void InitializeComponent()
{
//InitializeComponent
}
}

然后从UITableViewSource派生Source

public class Source : UITableViewSource
{
protected RootElement Root; public Source(RootElement root)
{
Root = root;
} public override Int32 RowsInSection(UITableView tableview, Int32 section)
{
return Root[section].Elements.Count;
} public override Int32 NumberOfSections(UITableView tableView)
{
return Root.Count;
} public override String TitleForHeader(UITableView tableView, Int32 section)
{
return Root[section].Caption;
} public override String TitleForFooter(UITableView tableView, Int32 section)
{
return Root[section].Footer;
} public override UITableViewCell GetCell(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
var section = Root[indexPath.Section];
var element = section.Elements[indexPath.Row]; return element.GetCell(tableView);
} public override void WillDisplay(UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
{
if (Root.NeedColorUpdate)
{
var section = Root[indexPath.Section];
var element = section.Elements[indexPath.Row];
var colorized = element as MonoTouch.Dialog.IColorizeBackground;
if (colorized != null)
colorized.WillDisplay(tableView, cell, indexPath);
}
} public override void RowDeselected(UITableView tableView, NSIndexPath indexPath)
{
var section = Root[indexPath.Section];
var element = section.Elements[indexPath.Row]; element.Deselected(null, tableView, indexPath);
} public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var section = Root[indexPath.Section];
var element = section.Elements[indexPath.Row]; element.Selected(null, tableView, indexPath);
} public override UIView GetViewForHeader(UITableView tableView, Int32 sectionIdx)
{
var section = Root[sectionIdx];
return section.HeaderView;
} public override Single GetHeightForHeader(UITableView tableView, Int32 sectionIdx)
{
var result = 0.001f;//本来应该是0,但是在ios7或以上版本在tableview的头部和底部或出现大约44px的空白。未解之谜!!!
var section = Root[sectionIdx];
if (!String.IsNullOrEmpty(section.Caption) || !String.IsNullOrEmpty(section.Header))
result = 30f;
if (section.HeaderView != null)
result = section.HeaderView.Frame.Height; return result;
} public override UIView GetViewForFooter(UITableView tableView, Int32 sectionIdx)
{
var section = Root[sectionIdx];
return section.FooterView;
} public override float GetHeightForFooter(UITableView tableView, Int32 sectionIdx)
{
var result = 0.001f;//本来应该是0,但是在ios7或以上版本在tableview的头部和底部或出现大约44px的空白。未解之谜!!!
var section = Root[sectionIdx];
if (!String.IsNullOrEmpty(section.Footer))
result = 30f;
if (section.FooterView != null)
result = section.HeaderView.Frame.Height;
return result;
}
}

最后从Source派生SizingSource

public class SizingSource : Source
{
public SizingSource(RootElement root) : base(root) { } public override float GetHeightForRow(UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
var section = Root[indexPath.Section];
var element = section.Elements[indexPath.Row]; var sizable = element as MonoTouch.Dialog.IElementSizing;
if (sizable == null)
return tableView.RowHeight;
return sizable.GetHeight(tableView, indexPath);
}
}

用了觉得好,请点个赞。

简化MonoTouch.Dialog的使用的更多相关文章

  1. 使用MonoTouch.Dialog简化iOS界面开发

    MonoTouch.Dialog简称MT.D,是Xamarin.iOS的一个RAD工具包.它提供易于使用的声明式API,不需要使用导航控制器.表格等ViewController来定义复杂的应用程序UI ...

  2. MonoTouch.Dialog简介

    新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...

  3. 【Xamarin挖墙脚系列:Mono项目的图标为啥叫Mono】

    因为发起人大Boss :Miguel de lcaza 是西班牙人,喜欢猴子.................就跟Hadoop的创始人的闺女喜欢大象一样...................... 历 ...

  4. mono touch登录设计

    需要对MonoTouch.Dialog-1进行引用: using System; using System.Collections.Generic; using System.Linq; using ...

  5. 3java面试题 传智 发的 有用

    第一章内容介绍 20 第二章JavaSE基础 21 一.Java面向对象 21 1. 面向对象都有哪些特性以及你对这些特性的理解 21 2. 访问权限修饰符public.private.protect ...

  6. bootstrap dialog 使用模态对话框

    bootstrap3-dialog 使用模态对话框 <div class="modal fade"> <div class="modal-dialog& ...

  7. keyboard dialog 仿微博表情键盘输入框

    功能描述:弹出键盘,右边有最大化按钮,没有最大化的时候最大行数为3,默认1行,随着文字输入增加自动增加高度,到达3行时不会在增加,点击最大化输入框,行数无限制,输入框下方图片点击删除 此处为了简便,键 ...

  8. 关于EasyUI datagrid 无法在dialog中显示的问题分析及解决方案!

    最近项目中引用了easyUI,很大程度上的简化了开发过程,但是随之而来的也遇到一些问题,比如:标题中遇到的问题,去网上搜罗了下关于这个问题的解决方案,不是说的很复杂就是干脆文不对题,国外的使用这种稍微 ...

  9. dialog - 从 shell 显示对话框

    总览 (SYNOPSIS) dialog --clear dialog --create-rc file dialog --print-maxsize dialog common-options bo ...

随机推荐

  1. dotNET跨平台相关文档

    dotNET跨平台相关文档整理 一直在从事C#开发的相关技术工作,从C# 1.0一路用到现在的C# 6.0, 通常情况下被局限于Windows平台,Mono项目把我们C#程序带到了Windows之外的 ...

  2. Poj Roadblocks(次短路)

    Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...

  3. this、访问修饰符——Java笔记(五)

    this         表示当前对象         谁调用方法谁就是当前对象 用static修饰的代码块里面不能使用this 方法里面有一个和字段同名的局部变量时,不能省略this   this还 ...

  4. 向西项目管理工具Maven一片

    前言 相信仅仅要做过 Java 开发的童鞋们,对 Ant 想必都不陌生,我们往往使用 Ant 来构建项目,尤其是涉及到特别繁杂的工作量.一个 build.xml 可以完毕编译.測试.打包.部署等非常多 ...

  5. C#:winform项目在win7,xp32位和64位都能执行

    vs中项目配置管理器活动解决方式平台选择X86平台.

  6. DirectX11 学习笔记3 - 创建一个立方体 和 轴

    该方案将在进一步的程序 面向对象. 独立的模型类.更像是一个框架. 其中以超过遇到了一个非常有趣的问题,.获得一晚.我读了好几遍,以找到其他的列子.必须放在某些功能Render里面实时更新,而不是仅仅 ...

  7. 私人定制javascript事件处理机制(浅谈)

    看到园子里关于事件监听发表的文章,我都有点不好意思写了.不过想想我的题目以私人定制作开头也就妥妥地写吧. 事件相关概念 1.事件类型 发生事件的字符串 有传统事件类型 比如表单.window事件等 D ...

  8. 百度地图API 添加自定义标注 多点标注

    原文:百度地图API 添加自定义标注 多点标注 分四个文件 location.php map.css 图片 数据库 数据库配置自己改下 -------------------------------- ...

  9. krpano音量控制(我们已经转移到krpano中国网站 krpano360.com)

    需求: 实现音量控制,这是官网的样例, 本文已经转移 到 krpano中文网 p=148">http://krpano360.com/? p=148 很多其它教程关注微信公众号 krp ...

  10. 【百度地图API】建立全国银行位置查询系统(五)——如何更改百度地图的信息窗口内容?

    原文:[百度地图API]建立全国银行位置查询系统(五)--如何更改百度地图的信息窗口内容? 摘要: 酷讯.搜房.去哪儿网等大型房产.旅游酒店网站,用的是百度的数据库,却显示了自定义的信息窗口内容,这是 ...