public
Created

RadioElement for MonoTouch.Dialog - autopops

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
public class RadioElement : StringElement {
public string Group;
internal int RadioIdx;
public bool PopOnSelect = false;
private bool ShouldDismiss = false;
public RadioElement (string caption, string group) : base (caption)
{
Group = group;
}
public RadioElement (string caption) : base (caption)
{
}
public RadioElement(string caption, bool popAutomatically, bool shouldDismiss = false) : base(caption)
{
PopOnSelect = popAutomatically;
ShouldDismiss = shouldDismiss;
}
 
public override UITableViewCell GetCell (UITableView tv)
{
var cell = base.GetCell (tv);
//var root = (RootElement) Parent.Parent;
var root = GetImmediateRootElement();
bool selected = false;
//if (root != null)
//{
if (!(root.group is RadioGroup))
throw new Exception ("The RootElement's Group is null or is not a RadioGroup");
selected = RadioIdx == ((RadioGroup)(root.group)).Selected;
//}
cell.Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None;
cell.SelectionStyle = UITableViewCellSelectionStyle.Blue;
return cell;
}
 
public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
{
var root = GetImmediateRootElement();
if (RadioIdx != root.RadioSelected){
var cell = tableView.CellAt (root.PathForRadio (root.RadioSelected));
if (cell != null)
cell.Accessory = UITableViewCellAccessory.None;
cell = tableView.CellAt (indexPath);
if (cell != null)
cell.Accessory = UITableViewCellAccessory.Checkmark;
root.RadioSelected = RadioIdx;
}
base.Selected (dvc, tableView, indexPath);
if (PopOnSelect)
{
if (ShouldDismiss)
{
dvc.NavigationController.DismissModalViewControllerAnimated(true);
} else {
dvc.NavigationController.PopViewControllerAnimated(true);
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.