Skip to content

Commit

Permalink
Allow users to override the Source for the DialogViewController
Browse files Browse the repository at this point in the history
Allow users to control whether UIViewElements can be selected
Small bug fix for cases where we need to redisplay
  • Loading branch information
Miguel de Icaza authored and Miguel de Icaza committed May 23, 2010
1 parent 51b2b5d commit f7d3617
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
54 changes: 30 additions & 24 deletions MonoTouch.Dialog/DialogViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,77 +249,77 @@ 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;
}

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

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;
}

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;
Expand All @@ -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);
Expand All @@ -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
}
Expand All @@ -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;
Expand Down Expand Up @@ -486,14 +487,19 @@ public override void ViewWillAppear (bool animated)
}
}

public virtual Source CreateSizingSource (bool unevenRows)
{
return unevenRows ? new SizingSource (this) : new Source (this);
}

Source TableSource;

void UpdateSource ()
{
if (root == null)
return;

TableSource = root.UnevenRows ? new SizingSource (this) : new Source (this);
TableSource = CreateSizingSource (root.UnevenRows);
tableView.Source = TableSource;
}

Expand Down
15 changes: 12 additions & 3 deletions MonoTouch.Dialog/Elements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public void UpdateFrom (BaseBooleanImageElement newParent)
parent = newParent;
UpdateImage ();
label.Text = parent.Caption;
SetNeedsDisplay ();
}
}

Expand Down Expand Up @@ -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
}

/// <summary>
/// Constructor
Expand All @@ -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++);
}

Expand All @@ -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;

//
Expand All @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------
Expand Down Expand Up @@ -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.

0 comments on commit f7d3617

Please sign in to comment.