[英]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.