在Xamarin.iOS中创建分组UITableview

在Xamarin.iOS中创建分组UITableview,ios,xamarin,Ios,Xamarin,如何在UI表视图中创建分组。选中我想在xamarin.iOS中实现的图像 您必须创建一个表源类,它保存您的数据并设置节的标题(在iOS中称为节) 您需要覆盖以下方法: public override string TitleForHeader(UITableView tableView, nint section) { return SessionGroups == null ? string.Empty : SessionGroups[(int)section]

如何在UI表视图中创建分组。选中我想在xamarin.iOS中实现的图像


您必须创建一个表源类,它保存您的数据并设置节的标题(在iOS中称为节)

您需要覆盖以下方法:

    public override string TitleForHeader(UITableView tableView, nint section)
    {
        return SessionGroups == null ? string.Empty : SessionGroups[(int)section].Key;
    }

    public override nint NumberOfSections(UITableView tableView)
    {
        return SessionGroups == null ? 0 : SessionGroups.Count;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return SessionGroups == null ? 0 : SessionGroups[(int)section].Count;
    }

    public override string[] SectionIndexTitles(UITableView tableView)
    {
        return SessionGroups == null ? new string[0] : SessionGroups.Select(x => x.Key).ToArray();
    }
你可以找到更多的信息。在“添加索引”和“添加页眉和页脚”部分

编辑

数据源结构也很重要。您需要像keyedlist(或者上面链接中显示的带有列表的字典)这样的东西


谢谢joehi。我明白你的意思。但是我怎样才能按天分组呢?我有一个数据集,每天有一列。我想将其分组。我用其他信息更新了我的答案。也许这可以告诉你你可以选择的方向,为什么不能添加ObserviekEyedList的引用?如果我使用KeyedCollection,则SessionGroups[(int)节]。键不起作用:(
ObservableKeyDList
是我创建的自定义cluss。源代码在我的答案中。只要将此类添加到您的解决方案中,它就会工作。非常感谢您。也请共享控制器端代码。我有点困惑。在使用之前:List tableItems=new List();然后填写此列表tblCourse.source=new CourseDetailTableSource(这个,tableItems);如果您已经实现了它,请添加完整的代码和答案..我在实现中遇到了问题,您会很好的。
protected IList<ObservableKeyedList<string, string>> SessionGroups;

// Just override the ItemsSource property (base class we use: MvxStandardTableViewSource)
public new IList<ObservableKeyedList<string, string>> ItemsSource
{
    get { return SessionGroups; }
    set
    {
        SessionGroups = value;
        ReloadTableData();
    }
}
public class ObservableKeyedList<TKey, TItem> : ObservableCollection<TItem>
    {
        public TKey Key { protected set; get; }

        public ObservableKeyedList(TKey key, IEnumerable<TItem> items) : base(items)
        {
            Key = key;
        }

        public ObservableKeyedList(IGrouping<TKey, TItem> grouping) : base(grouping)
        {
            Key = grouping.Key;
        }
    }