2017-10-20 1 views
0

두 개의 단추가있는 접을 수있는 목록을 원합니다. UITableViewHeaderFooterView을 사용해 보았지만 작동하지 않았습니다. 목록 내용이 겹칩니다. 다음은 내가 사용한 코드입니다.xamarin ios에서 접을 수있는보기

public class ExpandableTableCell : UITableViewHeaderFooterView 
    { 
     toggleSection delegates; 
     int section; 

     public object SelectHederAction { get; } 

     public ExpandableTableCell(string title, int section)//, toggleSection del) 
     { 
      this.TextLabel.Text = title; 
      this.section = section; 
      //this.delegates = del; 
     } 
     //UITableViewHeaderFooterView(NSString reuseIdentifier); 

     public ExpandableTableCell(NSString reuseIdentifier) : base(reuseIdentifier) 
     { 
      this.AddGestureRecognizer(UITapGestureRecognizer(this, SelectHederAction)); 
     } 


     public ExpandableTableCell(NSCoder coder) : base(coder) 
     { 

     } 

     private UIGestureRecognizer UITapGestureRecognizer(ExpandableTableCell expandableTableCell, object selectHederAction) 
     { 
      delegates(expandableTableCell, expandableTableCell.section); 

      return null; 
     } 

     public override void LayoutSubviews() 
     { 
      base.LayoutSubviews(); 
      this.TextLabel.TextColor = UIColor.White; 
      this.ContentView.BackgroundColor = UIColor.DarkGray;  

     } 

    } 




public partial class ViewController : UIViewController, IUITableViewDataSource, IUITableViewDelegate 
    { 


     UITableView table; 
     List<Sections> sectionList = new List<Sections>(); 
     public ViewController(IntPtr handle) : base(handle) 
     { 
      List<string> lstHorror = new List<string>(); 
      lstHorror.Add("Horror 1"); 
      lstHorror.Add("Horror 2"); 
      lstHorror.Add("Horror 3"); 
      sectionList.Add(new Sections("Horror", lstHorror, false)); 
      List<string> lstComedy = new List<string>(); 
      lstHorror.Add("Comedy 1"); 
      lstHorror.Add("Comedy 2"); 
      lstHorror.Add("Comedy 3"); 
      sectionList.Add(new Sections("Comedy", lstComedy, false)); 
      table = new UITableView(View.Bounds); 
      this.View.Add(table); 
      table.WeakDataSource = this;// new MeasurementsDetailsTableSource(sectionList, this.table); 
      table.WeakDelegate = this; 


     } 



     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 
      // Perform any additional setup after loading the view, typically from a nib. 
     } 

     public override void DidReceiveMemoryWarning() 
     { 
      base.DidReceiveMemoryWarning(); 
      // Release any cached data, images, etc that aren't in use. 
     } 

     [Export("tableView:numberOfRowsInSection:")] 
     public nint RowsInSection(UITableView tableView, nint section) 
     { 
      return sectionList[Int32.Parse(section.ToString())].Movies.Count; 
     } 
     [Export("tableView:cellForRowAtIndexPath:")] 
     public UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
     { 
      string CellIdentifier = "celltest"; 
      UITableViewCell cell = tableView.DequeueReusableCell(CellIdentifier); 
      string item = sectionList[Int32.Parse(indexPath.Section.ToString())].Movies[Int32.Parse(indexPath.Row.ToString())]; 
      if (cell == null) 
      { 
       cell = new UITableViewCell(UITableViewCellStyle.Subtitle, CellIdentifier); 
      } 
      cell.FocusStyle = UITableViewCellFocusStyle.Default; 

      cell.TextLabel.Text = item; 
      return cell; 
     } 
     [Export("numberOfSectionsInTableView:")] 
     public nint NumberOfSections(UITableView tableView) 
     { 
      return sectionList.Count; 
     } 

     //[Export("tableView:heightForHeaderInSection:")] 
     //public virtual float GetHeightForHeader(UITableView tableView, int section) 
     //{ 
     // return 100f;// *section; 
     //} 
     [Export("tableView:heightForFooterInSection:")] 
     public virtual float GetHeightForFooter(UITableView tableView, int section) 
     { 
      return 2f; 
     } 

     [Export("tableView:viewForHeaderInSection:")] 
     public virtual UIView GetViewForHeader(UITableView tableView, int section) 
     { 
      var header = new ExpandableTableCell(sectionList[section].Genere, section);//, this.toggleSection); 
      return header; 
     } 




     [Export("tableView:heightForRowAtIndexPath:")] 
     public virtual float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) 
     { 
      if (sectionList[Int32.Parse(indexPath.Section.ToString())].Expandable==true) 
      { 
       return 44;// * indexPath.Row; 

      } 
      else 
      { 
       return 0f; 
      } 
     } 

     //[Export("tableView:titleForHeaderInSection:")] 
     //public string TitleForHeader(UITableView tableView, int section) 
     //{ 
     // return "Header"; 
     //} 

     //public string TitleForFooter(UITableView tableView, int section) 
     //{ 
     // return "Footer"; 
     //} 

     private void toggleSection(ExpandableTableCell header, int section) 
     { 
      sectionList[section].Expandable = !sectionList[section].Expandable; 
      for (int i = 0; i < sectionList[section].Movies.Count; i++) 
      { 
       table.BeginUpdates(); 
       NSIndexPath[] rowsToReload = new NSIndexPath[] { 
        NSIndexPath.FromRowSection(i, section) 
       }; 
       table.ReloadRows(rowsToReload, UITableViewRowAnimation.None); 
       table.ReloadData(); 
       table.EndUpdates(); 
      } 
     } 

     //public nint RowsInSection(UITableView tableView, nint section) 
     //{ 
     // return sectionList[Int32.Parse(section.ToString())].Movies.Count; 
     //} 
    } 

다음은 내가 찾던의 예는 다음과 같습니다 Example image

편집 및 삭제 버튼 만 특정 목록 항목의 클릭에 볼 수 있습니다.

+0

안녕하세요, native iOS dev 여기에서, 달성하고자하는 이미지를 게시 할 수 있습니까? – Glenn

+0

@ 글렌, 빠른 답장을 보내 주셔서 감사합니다. 편집 된 질문을 살펴보십시오. – Arti

답변

0

RowSelectedGetHeightForRow 대리인을 사용하여이를 달성 할 수 있습니다.

당신의 tableview에 대리자를 할당합니다

this.TableView.Delegate = new MyDelegate(); 

UITableViewDelegate의 서브 클래스에서 RowSelectedGetHeightForRow 대표를 구현, 다음과 같이 :

public class MyDelegate : UITableViewDelegate 
{ 
    private bool hasCellExpanded = false; 
    private int row; 

    public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 
    { 

     row = indexPath.Row; 

     if(hasCellExpanded){ 

      hasCellExpanded = false; 

     }else{ 
      hasCellExpanded = true; 
     } 

     tableView.BeginUpdates(); 
     tableView.EndUpdates(); 

    } 


    public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath) 
    { 

     if (row == indexPath.Row&&hasCellExpanded){ 
      return 100; 
     }else{ 

      return 50; 
     } 

    } 
} 

그것은 다음과 같이 작동

enter image description here

+0

@Arti, 문제를 해결 했습니까? –

관련 문제