xamarin.ios - 单点触控对话框在单个 View 上的多个 radio 组(单个根元素)

标签 xamarin.ios monotouch.dialog

我想在带有单点触控对话框的单个根元素中使用多个 radio 组。每个 radio 组都有自己的部分。我找不到一种方法来完成这项工作,因为只能将单个 radio 组分配给根元素

最佳答案

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

希望对您有所帮助!

关于xamarin.ios - 单点触控对话框在单个 View 上的多个 radio 组(单个根元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13608810/

相关文章:

ios - MVVMCross + SQLCipher = 不可能?

iphone - MonoTouch.Dialog 中的自定义 UIPickerView?

带有 2 行标签的 Xamarin.Forms ListView ?

c# - 如何在 DialogViewController 上将背景设置为透明

ios - Monotouch.Dialog IOS 6 Split View旋转问题

ios - 在 iOS 中捕获签名时出现性能问题

c# - 调用 method_getImplementation 在设备上抛出 ExecutionEngineException

c# - ViewDidAppear 导致我的标签消失(Xamarin.ios,C#)

ios - 限制每个 View Controller 的方向

uiview - 如何在 MonoTouch 中使 UIView 可点击?