Xamarin UITableView行扩展行选择 - 可能的iOS?

时间:2014-08-12 08:59:55

标签: c# ios uitableview xamarin.ios xamarin

我正在研究Xamarin可能将当前的项目转移到C#标准。以下是专门针对iOS的。 目前的一个重要要求如下:

  • 设置表或列表的数据源
  • 选择列表项后,该行将展开或折叠显示其相关数据
  • 列表中的每个项目都有不同的布局

我发现上述步骤可能有表视图,但是,不清楚是否可以扩展行。我发现这个YouTube video展示了与所需要的相似的功能,但这是在目标C中,但我目前不熟悉这种语言,并且不确定xamarin与目标C相比的局限性。

我应该使用RowSelected吗?如果是,是否有基于数据调整高度的示例?

要求的其他例子是

简而言之,显示带有摘要标签的行,单击行,显示该行的更多数据。任何简单粗略的例子都会非常有用。

我愿意扩大高度,或者在显示和隐藏的那一行上有一行相关

非常感谢您提前:))

2 个答案:

答案 0 :(得分:0)

您可能已经得到了答案,但如果其他人有类似的问题,仍然值得发布回复。

让我们从:"设置表或列表的数据源。"

这当然可以在Forms中使用,也可以在单独的iOS和Android版本中使用。例如,在iOS中,您实际上为表视图创建了一个源类,它允许您遍历数据,访问行选择方法,并进行一系列有用的修改。

"选择列表项后,该行将展开或折叠显示其相关数据。"

是的,这是完全可能的,它们有几种方法可以实现它,你可以利用行选择的方法,只需扩展表格视图的高度来显示隐藏的内容,或者你可以创建一个子单元格,几乎喜欢手风琴效果。网上有很多指南。

最后"列表中的每个项目都有不同的布局"

这可以在创建表格单元格时迭代源代码时完成,并允许您根据属于数据对象的某种标记/标记更改内容。

答案 1 :(得分:0)

您可以使用tableviews部分和行。触摸某个部分时,它会展开/折叠以显示/隐藏行。使用List来跟踪是否扩展了一个部分。这是TableViewSource的一个例子。我认为代码非常自我解释。

public class MyTableSource : UITableViewSource
{
    string SectionIndentifer = "mySection";
    string CellIndentifer = "myCell";

    public List<MyItem> vList { get; set; }
    List<bool> expandStatusList; // Maintain a list which keeps track if a section is expanded or not

    public MeasurementsDetailsTableSource (List<MyItem> vList)
    {
        this.vList = vList;
        expandStatusList = new List<bool> ();

        for (int i = 0; i < vList.Count; i++) {
            expandStatusList.Add (false); // Initially, no section are expanded
        } 
    }

    public override nint NumberOfSections (UITableView tableView)
    {
        return vList.Count;
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        if (!expandStatusList [(int)section])
            return 0; // Returning 0 to hide all the rows of the section, there by collapsing the section.
        return vList [(int)section].subItems.Count; 
    }

    public override UIView GetViewForHeader (UITableView tableView, nint section)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (SectionIndentifer);
        if (cell == null) {
            cell = new UITableViewCell (UITableViewCellStyle.Default, SectionIndentifer);
        }
        ...
        // Settings up a click event to section to expand the section..

        var gesture = new UITapGestureRecognizer ();
        gesture.AddTarget (() => ParentClick (gesture));
        cell.AddGestureRecognizer (gesture);

        cell.Tag = section; // This is needed to indentify the section clicked..

        return cell;
     }

     public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
     {
         UITableViewCell cell = tableView.DequeueReusableCell (CellIndentifer);
         if (cell == null) {
            cell = new UITableViewCell (UITableViewCellStyle.Default, CellIndentifer);
         }
         ...
         return cell;
     }

     public void ParentClick (UITapGestureRecognizer gesture)
     {

         NSIndexPath indexPath = NSIndexPath.FromRowSection (0, gesture.View.Tag);
         if (indexPath.Row == 0) {
             if (vList [indexPath.Section].mValues.Count != 0) {
                 bool expandStatus = expandStatusList [indexPath.Section];
                 for (int i = 0; i < vList.Count; i++) {
                     if (indexPath.Section == i) {
                         expandStatusList [i] = !expandStatus;
                     }
                 }
                 tableView.ReloadSections (NSIndexSet.FromIndex (gesture.View.Tag), UITableViewRowAnimation.Automatic);
                 tableView.ReloadData ();
                 return;
             }
         }
     }
 }