This is a migrated thread and some comments may be shown as answers.

DataSource grouping by NSDate

3 Answers 109 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sal
Top achievements
Rank 1
Sal asked on 27 Oct 2015, 09:06 PM
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). 

3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 28 Oct 2015, 10:12 AM
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
0
Sal
Top achievements
Rank 1
answered on 28 Oct 2015, 03:07 PM

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

0
Jack
Telerik team
answered on 29 Oct 2015, 09:35 AM
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
Tags
DataForm
Asked by
Sal
Top achievements
Rank 1
Answers by
Jack
Telerik team
Sal
Top achievements
Rank 1
Share this question
or