Skip to content

Instantly share code, notes, and snippets.

@nicwise
Created September 1, 2012 11:07
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nicwise/3569920 to your computer and use it in GitHub Desktop.
RadioElement for MonoTouch.Dialog - autopops
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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment