读了一位园友写的使用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. c语言获取符号位整数和浮点

    1. 为什么你应该得到的签位 非常多的时间,我们需要推断的数目值正和负,做了相应的逻辑处理.完成这一要求条件推断语句可以很好. 有时会出现以下情况, if (x > 0) { x = x - 1 ...

  2. Lake Counting (DFS)

    N*M的园子,雨后积起了水.八连通的积水背认为是连接在一起的.请求出园子里总共有多少水洼? dfs(Depth-First  Search)  八个方向的简单搜索.... 深度优先搜索从最开始的状态出 ...

  3. 网络资源(9) - TDD视频

    2014_08_26 http://v.youku.com/v_show/id_XMzI4Mzk1MjQ4.html TDD测试驱动开发

  4. VC各种方法获得的窗口句柄

    AfxGetMainWnd AfxGetMainWnd获取窗口句柄本身 HWND hWnd = AfxGetMainWnd()->m_hWnd; GetTopWindow 功能:子窗体z序(Z序 ...

  5. 从头开始学JavaScript (二)——变量及其作用域

    原文:从头开始学JavaScript (二)--变量及其作用域 一.变量 ECMAscript变量是松散型变量,所谓松散型变量,就是变量名称可以保存任何类型的数据,每个变量仅仅是一个用于保存值的占位符 ...

  6. 基OOP知识

    从今天开始,我开始总结GAO还通高老师<android道路的建筑师>,尝试一个星期写三个博客. 相对而言.看到这篇文章有点速度比你可以观看视频,刚才看的视频是更具体的.假设有兴趣,跟着我去 ...

  7. 启用IIS7报错功能

    进入:控制面板 - 卸载程序 - 打开或关闭Windows功能 如果访问任何不存在页面或页面出错时空白: Internet 信息服务 - 万维网服务 - 常见 HTTP 功能 - HTTP 错误 打勾 ...

  8. SVN & Git (二)

    Git:是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.Git 是 Linus T ...

  9. IOS、java支持DES加密

    转载请注明博客地址:http://blog.csdn.net/mengxiangyue/article/details/40015727 近期在考虑数据加密方面的需求,所以对数据加密简单的看了一下,当 ...

  10. 经典HTML5小游戏 支持各种浏览器 (围住神经猫)

    源码地址: http://files.cnblogs.com/files/liujing379069296/MyCat.rar 插件地址:http://files.cnblogs.com/files/ ...