我想要一个带有2个按钮的可折叠列表,我尝试使用UITableViewHeaderFooterView
,但它没有用。列表内容重叠。以下是我使用的代码。
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;
//}
}
只有在点击特定列表项时才会显示编辑和删除按钮。
答案 0 :(得分:0)
您可以使用RowSelected
和GetHeightForRow
代表来实现这一目标。
将代表分配到您的tableview:
this.TableView.Delegate = new MyDelegate();
在RowSelected
的子类中实施GetHeightForRow
和UITableViewDelegate
个委托,如下所示:
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;
}
}
}
它的工作原理如下: