简体   繁体   中英

Create Grouped UITableview in Xamarin.iOS

How I can create grouped in UI Table View. Check image I want to implement same in xamarin.iOS.

在此处输入图片说明

You have to create a TableSource -Class which is holding your data and sets the headers for the sections (they are called sections in iOS).

You need to override following methods:

    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();
    }

You can find more infos here . In the 'Adding an Index' and 'Adding Headers and Footers' section.

Edit

Important is also the datasource structure. You Need something like a keyedlist (or a dictionary with a list like showed in the above link).

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();
    }
}

The first string of the ObservableKeyedList is the key for your section/group. And the second string is the value you want to show on the UI: The second can also be a model with different properties.

So you just Need to order/group your data in the above structure and set the ItemsSource of your UiTableSource.. thats it =)

Heres the ObservableKeyedList we use:

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM