DataSource grouping by NSDate

4 posts, 0 answers
  1. Sal
    Sal avatar
    58 posts
    Member since:
    Feb 2015

    Posted 27 Oct 2015 Link to this post

    I didn't see a specific section for TKDataSource so I'll just put this here for now. I'm trying to use the TKDataSource to group various items by a date, and the grouping works but it's not filtered in ascending/descending order and even if I add a sort option it doesn't take it. A second issue is if I use the TKDataSource as a UITableView datasource is there a way to update the text used as the grouping key (again I'm using a NSDate, but it prints the full date and I would want to use a shorter date string for the section header). 
  2. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 28 Oct 2015 Link to this post

    Hi Sal,

    Currently TKDataSource does not provide an option to sort group headers. The sorting applies only on items. The issue is logged on our feedback portal and it will be addressed in one of our upcoming releases. Here is a link to the issue.

    You can customize group headers text in TKDataSource by inheriting the class and overriding its tableView:titleForHeaderInSection: method. Consider the following sample, it assumes that the group key contains NSDate object:
    @interface MyDataSource : TKDataSource
    @end
     
    @implementation MyDataSource
     
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        TKDataSourceGroup *group = (TKDataSourceGroup*)self.items[section];
        NSDateFormatter *formatter = [NSDateFormatter new];
        formatter.dateFormat = @"MM/DD/YYYY";
        return [formatter stringFromDate:group.key];
    }
     
    @end

    Do not hesitate to contact us if you have further questions.

    Regards,
    Jack
    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. Sal
    Sal avatar
    58 posts
    Member since:
    Feb 2015

    Posted 28 Oct 2015 in reply to Jack Link to this post

    Thanks Jack, but even when I try using the tableView titleForHeaderInSection function it doesn't change the header of the cell. Actually it doesn't even get called since I tried just a simple string and nothing changed. Unless I'm doing something else that is wrong. Here's my current setup for this issue (all in Swift as well)

     

      This is in my viewDidLoad, I just take the array of items and set them as the itemSource for my TKDataSource and then add the sort and group keys before setting it as the tableViews DataSource. I removed all the code for the data source cells since it's not really relevant to this issue as that part works fine. 

              self.dataSource.itemSource = taskItems
           self.dataSource.sortWithKey("startDate", ascending: true)
           self.dataSource.groupWithKey("startDate")
           self.mainTable.dataSource = self.dataSource
     
           // create cell for TKDataSource
           self.dataSource.settings.tableView.createCell { (UITableView tableView, NSIndexPath indexPath, AnyObject item) -> UITableViewCell in
               //setup custom cell
               .....
               return customCell!
           }

     

     I then added this simple title just to test it out and the group title still shows the full NSDate from the group key. 

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return "ABC"
        }

  5. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 29 Oct 2015 Link to this post

    Hello Sal,

    Could you confirm that you have replaced the default TKDataSource with MyDataSource from the code snippet? I prepared a sample project to demonstrate this approach. Find it attached here.

    Regards,
    Jack
    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