From f7d36172860c86cdc2d6bc5c1a8f8b38e28f3597 Mon Sep 17 00:00:00 2001 From: Miguel de Icaza Date: Sun, 23 May 2010 14:49:41 -0400 Subject: [PATCH] Allow users to override the Source for the DialogViewController Allow users to control whether UIViewElements can be selected Small bug fix for cases where we need to redisplay --- MonoTouch.Dialog/DialogViewController.cs | 54 +++++++++++++----------- MonoTouch.Dialog/Elements.cs | 15 +++++-- README.markdown | 19 ++++++++- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/MonoTouch.Dialog/DialogViewController.cs b/MonoTouch.Dialog/DialogViewController.cs index 70e76be0..da953f3d 100644 --- a/MonoTouch.Dialog/DialogViewController.cs +++ b/MonoTouch.Dialog/DialogViewController.cs @@ -249,21 +249,21 @@ public override void CancelButtonClicked (UISearchBar searchBar) } } - class Source : UITableViewSource { + public class Source : UITableViewSource { const float yboundary = 65; - protected DialogViewController container; - protected RootElement root; + protected DialogViewController Container; + protected RootElement Root; bool checkForRefresh; public Source (DialogViewController container) { - this.container = container; - root = container.root; + this.Container = container; + Root = container.root; } public override int RowsInSection (UITableView tableview, int section) { - var s = root.Sections [section]; + var s = Root.Sections [section]; var count = s.Elements.Count; return count; @@ -271,22 +271,22 @@ public override int RowsInSection (UITableView tableview, int section) public override int NumberOfSections (UITableView tableView) { - return root.Sections.Count; + return Root.Sections.Count; } public override string TitleForHeader (UITableView tableView, int section) { - return root.Sections [section].Caption; + return Root.Sections [section].Caption; } public override string TitleForFooter (UITableView tableView, int section) { - return root.Sections [section].Footer; + return Root.Sections [section].Footer; } public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) { - var section = root.Sections [indexPath.Section]; + var section = Root.Sections [indexPath.Section]; var element = section.Elements [indexPath.Row]; return element.GetCell (tableView); @@ -294,18 +294,18 @@ public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Founda public override void RowSelected (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) { - container.Selected (indexPath); + Container.Selected (indexPath); } public override UIView GetViewForHeader (UITableView tableView, int sectionIdx) { - var section = root.Sections [sectionIdx]; + var section = Root.Sections [sectionIdx]; return section.HeaderView; } public override float GetHeightForHeader (UITableView tableView, int sectionIdx) { - var section = root.Sections [sectionIdx]; + var section = Root.Sections [sectionIdx]; if (section.HeaderView == null) return -1; return section.HeaderView.Frame.Height; @@ -313,13 +313,13 @@ public override float GetHeightForHeader (UITableView tableView, int sectionIdx) public override UIView GetViewForFooter (UITableView tableView, int sectionIdx) { - var section = root.Sections [sectionIdx]; + var section = Root.Sections [sectionIdx]; return section.FooterView; } public override float GetHeightForFooter (UITableView tableView, int sectionIdx) { - var section = root.Sections [sectionIdx]; + var section = Root.Sections [sectionIdx]; if (section.FooterView == null) return -1; return section.FooterView.Frame.Height; @@ -330,13 +330,14 @@ public override void Scrolled (UIScrollView scrollView) { if (!checkForRefresh) return; - if (container.reloading) + if (Container.reloading) return; - var view = container.refreshView; + var view = Container.refreshView; if (view == null) return; - var point = container.TableView.ContentOffset; + var point = Container.TableView.ContentOffset; + if (view.IsFlipped && point.Y > -yboundary && point.Y < 0){ view.Flip (true); view.SetStatus (RefreshViewStatus.PullToReload); @@ -353,13 +354,13 @@ public override void DraggingStarted (UIScrollView scrollView) public override void DraggingEnded (UIScrollView scrollView, bool willDecelerate) { - if (container.refreshView == null) + if (Container.refreshView == null) return; checkForRefresh = false; - if (container.TableView.ContentOffset.Y > -yboundary) + if (Container.TableView.ContentOffset.Y > -yboundary) return; - container.TriggerRefresh (); + Container.TriggerRefresh (); } #endregion } @@ -369,12 +370,12 @@ public override void DraggingEnded (UIScrollView scrollView, bool willDecelerate // probe *every* row for its size; Avoid this by creating a separate // model that is used only when we have items that require resizing // - class SizingSource : Source { + public class SizingSource : Source { public SizingSource (DialogViewController controller) : base (controller) {} public override float GetHeightForRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) { - var section = root.Sections [indexPath.Section]; + var section = Root.Sections [indexPath.Section]; var element = section.Elements [indexPath.Row]; var sizable = element as IElementSizing; @@ -486,6 +487,11 @@ public override void ViewWillAppear (bool animated) } } + public virtual Source CreateSizingSource (bool unevenRows) + { + return unevenRows ? new SizingSource (this) : new Source (this); + } + Source TableSource; void UpdateSource () @@ -493,7 +499,7 @@ void UpdateSource () if (root == null) return; - TableSource = root.UnevenRows ? new SizingSource (this) : new Source (this); + TableSource = CreateSizingSource (root.UnevenRows); tableView.Source = TableSource; } diff --git a/MonoTouch.Dialog/Elements.cs b/MonoTouch.Dialog/Elements.cs index 45be2418..431a1e57 100644 --- a/MonoTouch.Dialog/Elements.cs +++ b/MonoTouch.Dialog/Elements.cs @@ -272,6 +272,7 @@ public void UpdateFrom (BaseBooleanImageElement newParent) parent = newParent; UpdateImage (); label.Text = parent.Caption; + SetNeedsDisplay (); } } @@ -1206,7 +1207,12 @@ public class UIViewElement : Element, IElementSizing { static int count; NSString key; protected UIView View; - bool transparent; + public CellFlags Flags; + + public enum CellFlags { + Transparent = 1, + DisableSelection = 2 + } /// /// Constructor @@ -1224,7 +1230,7 @@ public class UIViewElement : Element, IElementSizing { public UIViewElement (string caption, UIView view, bool transparent) : base (caption) { this.View = view; - this.transparent = transparent; + this.Flags = transparent ? CellFlags.Transparent : 0; key = new NSString ("UIViewElement" + count++); } @@ -1233,7 +1239,7 @@ public override UITableViewCell GetCell (UITableView tv) var cell = tv.DequeueReusableCell (key); if (cell == null){ cell = new UITableViewCell (UITableViewCellStyle.Default, key); - if (transparent){ + if ((Flags & CellFlags.Transparent) != 0){ cell.BackgroundColor = UIColor.Clear; // @@ -1244,6 +1250,9 @@ public override UITableViewCell GetCell (UITableView tv) BackgroundColor = UIColor.Clear }; } + if ((Flags & CellFlags.DisableSelection) != 0) + cell.SelectionStyle = UITableViewCellSelectionStyle.None; + cell.ContentView.AddSubview (View); } return cell; diff --git a/README.markdown b/README.markdown index 0b33f6ba..5c578a28 100644 --- a/README.markdown +++ b/README.markdown @@ -631,7 +631,9 @@ email address input (The values of UIKeyboardType). UIViewElement ------------- -Use this element to quickly add a standard UIView as cell in a UITableView. +Use this element to quickly add a standard UIView as cell in a UITableView, +you can control whether the cell can be selected or whether it is transparent +by passing one of the CellFlags to the constructor. ActivityElement --------------- @@ -742,3 +744,18 @@ DialogViewController: ParentViewController.View.BackgroundColor = color; } } + +Another customization point is the following virtual methods in the +DialogViewController: + + public override Source CreateSizingSource (bool unevenRows) + +This method should return a subclass of DialogViewController.Source +for cases where your cells are evenly sized, or a subclass of +DialogViewController.SizingSource if your cells are uneven. + +You can use this override to capture any of the UITableViewSource +methods. For example, TweetStation uses this to track when the +user has scrolled to the top and update accordingly the number +of unread tweets. +