2012-11-28 1 views

답변

2

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

희망이 도움이됩니다!

+0

얻기 컴파일 오류 : \t는 \t는 오류 CS1061 \t : 유형 'MonoTouch.UIKit.UITableViewCell가와'형 'MonoTouch.UIKit.UITableViewCell의'더 확장 메서드 'ApplyStyle''ApplyStyle에 대한 정의를 포함하지 않는 것은 '볼 수 있습니다 . CustomRadioElement.cs (61,61) : 오류 CS0103 : 'Fonts'이름이 현재 컨텍스트에 존재하지 않습니다. (CS0103) – svn

+0

이 종류의 컴파일 오류 줄 (ApplyStyle 및 Font)을 제거하면 작동합니다. 그러나 각 그룹에서 선택한 값을 미리 설정하려면 어떻게합니까? 또한 행을 강조 표시 할 때 강조 표시 – svn

+0

죄송합니다, svn! 내 코드를 수정했다. (확장 메소드'ApplyStyle'은 스타일을 셀에 적용한다.) 'customRootElement.Selected ("groupName")'groupName' 그룹에서 선택된 인덱스를 반환합니다. –

관련 문제