[英]Create Grouped UITableview in Xamarin.iOS
您必须创建一个TableSource -Class来保存数据并设置各节的标题(在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();
}
您可以在此处找到更多信息。 在“添加索引”和“添加页眉和页脚”部分中。
编辑
重要的是数据源结构。 您需要类似键控列表(或具有上面链接中显示的列表的字典)之类的东西。
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();
}
}
ObservableKeyedList
的第一个字符串是您的部分/组的键。 第二个字符串是要在UI上显示的值:第二个字符串也可以是具有不同属性的模型。
因此,您只需要在上述结构中对数据进行排序/分组并设置UiTableSource的ItemsSource。
这是我们使用的ObservableKeyedList:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.