Alphabetically grouped ContactList through TKListView

4 posts, 0 answers
  1. Burzin
    Burzin avatar
    4 posts
    Member since:
    Sep 2015

    Posted 09 Feb Link to this post

    Actually I am using a Telerik ListView to create a contactList which will be grouped according to the initials of the contact name as illustrated in the attachment. I cant figure out how to achieve this. Please help!
  2. Adrian
    Admin
    Adrian avatar
    167 posts

    Posted 11 Feb Link to this post

    Hello, Burzin,

    Thank you for contacting us.

    You can achieve this scenario using TKDataSource as data source to the list view and add a group descriptor. Consider the code snippet below:
    _dataSource = [TKDataSource new];
        [_dataSource addGroupDescriptor:[[TKDataSourceGroupDescriptor alloc] initWithBlock:^id(id item){
            // return group key - the first letter from the name
            NSString *key = [(NSString *)item substringToIndex:1];
            return key;
        } comparator:^NSComparisonResult(TKDataSourceGroup *obj1, TKDataSourceGroup *obj2) {
            // compare group keys so they can be in alphabetical order
            NSComparisonResult result = [obj1.key compare:obj2.key];
            return result;
        }]];
        [_dataSource loadDataFromJSONResource:@"ListViewSampleData" ofType:@"json" rootItemKeyPath:@"players"];

    And then set this data source to the dataSource property of the list view:
    listView.dataSource = _dataSource;

    I hope this helps. Do not hesitate to contact us in case you need further assistance.

    Regards,
    Adrian
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Burzin
    Burzin avatar
    4 posts
    Member since:
    Sep 2015

    Posted 11 Feb in reply to Adrian Link to this post

    Can we use TKDataSource for custom Cells of TKListView ? Also I am using Xamarin so can you please post c# code.
    Thanks
  5. Adrian
    Admin
    Adrian avatar
    167 posts

    Posted 17 Feb Link to this post

    Hi, Burzin,

    I apologize for the late response.

    Yes you can use TKDataSource to return custom list view cells. You should use the CreateCell method. Consider the code below:
    this.dataSource.Settings.ListView.CreateCell (delegate (TKListView list, NSIndexPath indexPath, NSObject item){
        // return your cell;
    });

    Here is the code snippet from my previous response written in C#:
    TKDataSourceGroupDescriptor descriptor = new TKDataSourceGroupDescriptor (delegate (NSObject item) {
                    NSString key = new NSString((item as NSString)[0].ToString());
                    return key;
                }, delegate (NSObject obj1, NSObject obj2) {
                    NSComparisonResult res = ((obj1 as TKDataSourceGroup).Key as NSString).Compare(((obj2 as TKDataSourceGroup).Key as NSString));
                    return res;
                });
     
                this.dataSource.AddGroupDescriptor (descriptor);
                this.dataSource.LoadDataFromJSONResource ("ListViewSampleData", "json", "players");

    I hope this helps. If you need further assistance do not hesitate to contact us.

    Regards,
    Adrian
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner