zoukankan      html  css  js  c++  java
  • 简化MonoTouch.Dialog的使用

    读了一位园友写的使用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);
            }
        }

    用了觉得好,请点个赞。

  • 相关阅读:
    ABAP 捕获回车键更新值
    拼接报错信息
    你知道什么叫金丝雀分布吗?你知道如何在Service Mesh微服务架构中实现吗?
    java代码在阿里云函数计算中的应用
    创投机构BP联系方式3
    迁移到RDS或者腾讯云MYSQL实例发生sql_mode=only_full_group_by错误的解决
    我国广告业存在的问题
    【微信小程序】固定头部,只滚动内容部分功能
    低智商屌丝蒟蒻的2012成都题解
    罪过啊,废了好大的力气才搞出了 Kth 数
  • 原文地址:https://www.cnblogs.com/odduncle/p/3471883.html
Copyright © 2011-2022 走看看