Alphabetically grouped ContactList through TKListView

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

    Posted 09 Feb 2016 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
    174 posts

    Posted 11 Feb 2016 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. Burzin
    Burzin avatar
    4 posts
    Member since:
    Sep 2015

    Posted 11 Feb 2016 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
  4. Adrian
    Admin
    Adrian avatar
    174 posts

    Posted 17 Feb 2016 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
  5. David
    David avatar
    1 posts
    Member since:
    Jan 2017

    Posted 06 Feb in reply to Adrian Link to this post

    I have problem with Group.

    I have name list. I've added "Group" column to my data. If I put only first letter to Group column, i see names grouped "a".."z". The names itself are NOT sorted!

    If I put FullName in Group, I see names SORTED, but each name has own group :(

    I tried GroupWithKey("Group", CompFunc) and AddGroupDescriptor(descriptor) like describes... nothing works. Sort on grouped DataSource also dont work.

  6. Deyan
    Admin
    Deyan avatar
    2080 posts

    Posted 08 Feb Link to this post

    Hello,

    Thanks for writing.

    To have the items sorted by name you will have to first add a Sort descriptor and sort by name, then add the Group descriptor and group by first letter. Remember that descriptors are processed in the order they are added.

    Here's more information:

    http://docs.telerik.com/devtools/ios/datasource/data-shaping

    Regards,
    Deyan
    Telerik by Progress
    Want to build beautiful Android apps as well? Check out UI for Android which enables the same set of scenarios, allowing you to create the same great app experience on both iOS and Android.
Back to Top