单点触控对话框 单个视图上的多个单选组(单个根元素)



我想在具有单点触摸对话框的单个rootelelement中使用多个无线电组。每个无线电组都有自己的部分。我找不到使这项工作的方法,因为单个单选组只能分配给根元素

svn!

这是我的解决方案

public class CustomRootElement : RootElement
{
    private RadioGroup _defaultGroup = new RadioGroup(0);
    private Dictionary<string, RadioGroup> _groups = new Dictionary<string, RadioGroup>();
    public CustomRootElement(string caption = "") : base(caption , new RadioGroup("default",0))
    {
    }
    public CustomRootElement(string caption, Group group, Func<RootElement, UIViewController> createOnSelected) : base(caption, group)
    {
        var radioGroup = group as RadioGroup;
        if(radioGroup != null)
        {
            _groups.Add(radioGroup.Key.ToLower(), radioGroup);
        }
        this.createOnSelected = createOnSelected;
    }

    public override UITableViewCell GetCell(UITableView tv)
    {
        var cell =  base.GetCell(tv);
        cell.SelectionStyle = UITableViewCellSelectionStyle.None;
        return cell;
    }
    public int Selected(string group)
    {
        if (string.IsNullOrEmpty(group))
        {
            throw new ArgumentNullException("group");
        }
        group = group.ToLower();
        if (_groups.ContainsKey(group))
        {
            return _groups[group].Selected;
        }
        return 0;
    }
    public void Select(string group, int selected)
    {
        if (string.IsNullOrEmpty(group))
        {
            throw new ArgumentNullException("group");
        }
        var radioGroup = GetGroup(group);
        radioGroup.Selected = selected;
    }
    internal RadioGroup GetGroup(string group)
    {
        if (string.IsNullOrEmpty(group))
        {
            throw new ArgumentNullException("group");
        }
        group = group.ToLower();
        if (!_groups.ContainsKey(group))
        {
            _groups[group] = new RadioGroup(group , 0);
        }
        return _groups[group];
    }
    internal NSIndexPath PathForRadioElement(string group, int index)
    {
        foreach (var section in this)
        {       
            foreach (var e in section.Elements)
            {
                var re = e as SlRadioElement;
                if (re != null 
                    && string.Equals(re.Group, group,StringComparison.InvariantCultureIgnoreCase)
                    && re.Index == index)
                {
                    return e.IndexPath;
                }
            }
        }
        return null;
    }
}

public class CustomRadioElement : RadioElement
{
    public event Action<CustomRadioElement> ElementSelected;
    private readonly static NSString ReuseId = new NSString("CustomRadioElement");
    private string _subtitle;
    public int? Index  { get; protected set; }
    public CustomRadioElement(string caption, string group = null, string subtitle = null) :base(caption, group)
    {
        _subtitle = subtitle;
    }
    protected override NSString CellKey
    {
        get
        {
            return ReuseId;
        }
    }
    public override UITableViewCell GetCell(UITableView tv)
    {
        EnsureIndex();
        var cell = tv.DequeueReusableCell(CellKey);
        if (cell == null)
        {
            cell = new UITableViewCell(UITableViewCellStyle.Subtitle , CellKey);
        }
        cell.ApplyStyle(this);
        cell.TextLabel.Text = Caption;
        if (!string.IsNullOrEmpty(_subtitle))
        {
            cell.DetailTextLabel.Text = _subtitle;
        }

        var selected = false;
        var slRoot = Parent.Parent as CustomRootElement;
        if (slRoot != null)
        {
            selected = Index == slRoot.Selected(Group);
        }
        else
        {
            var root = (RootElement)Parent.Parent;
            selected = Index == root.RadioSelected;
        }
        cell.Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.None;
        return cell;
    }
    public override void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
    {
        var slRoot = Parent.Parent as CustomRootElement;
        if (slRoot != null)
        {
            var radioGroup = slRoot.GetGroup(Group);
            if (radioGroup.Selected == Index)
            {
                return;
            }
            UITableViewCell cell;
            var selectedIndex = slRoot.PathForRadioElement(Group, radioGroup.Selected);
            if (selectedIndex != null)
            {
                cell = tableView.CellAt(selectedIndex);
                if (cell != null)
                {
                    cell.Accessory = UITableViewCellAccessory.None;
                }
            }

            cell = tableView.CellAt(indexPath);
            if (cell != null)
            {
                cell.Accessory = UITableViewCellAccessory.Checkmark;
            }
            radioGroup.Selected = Index.Value;

            var handler = ElementSelected;
            if (handler != null)
            {
                handler(this);
            }
        }
        else
        {
            base.Selected(dvc, tableView, indexPath);
        }
    }
    private void EnsureIndex()
    {
        if (!Index.HasValue)
        {
            var parent = Parent as Section;
            Index = parent.Elements.IndexOf(this);
        }
    }
}

希望这有帮助!

相关内容

最新更新