简化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);
        }
    }

用了觉得好,请点个赞。

原文地址:https://www.cnblogs.com/odduncle/p/3471883.html