2017-10-20 82 views
0

我想要一個包含2個按鈕的可摺疊列表,我嘗試過使用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

嗨,本地iOS開發在這裏,你可以發佈你想要實現的一些圖像嗎? – Glenn

+0

@Glenn,謝謝你的快速回復。請看看編輯過的問題。 – Arti

回答

0

您可以使用RowSelectedGetHeightForRow代表來實現這一點。

分配委託給你的實現代碼如下:

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,你解決了你的問題嗎? –