Welcome, Guest
Username Password: Remember me

Tableview section headers
(1 viewing) (1) Guest
  • Page:
  • 1
  • 2

TOPIC: Tableview section headers

Tableview section headers 1 year, 8 months ago #1

Hi,

This probably very obvious and stupid question, but I just can’t figure this out my own.

I’m using Core Data with Sensible Tableview and I have simple USER entity with bunch of attributes. One attribute is called NAME (surprisingly) which is showed in main SCTableView.

Everything else seems straight forward with this great framework, but how in earth can I add sections and sections header to my tableview?

I have done this earlier (with UITableView) using following code:

In UsersTableViewController.m:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}

...
NSFetchedResultsController *fetchedResultsController = [NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@”usersFirstLetter”
cacheName:@”Users”];

In Users.h
@interface User (UserAccessors)
- (NSString *)usersFirstLetter;
@end

In Users.m
-(NSString *) usersFirstLetter{
return [self.userName substringToIndex:1];
}

How can I do the same with SCTableView?

BR/jsaarinen
  • jsaarinen
  • OFFLINE
  • Fresh Boarder
  • Posts: 11
  • Karma: 0

Re: Tableview section headers 1 year, 8 months ago #2

Hi Jsaarinen,

With Sensible TableView, there are two very common kind of sections that you can use: SCArrayOfObjectsSection and SCObjectSection. As demonstrated in the sample Core Data App, SCArrayOfObjectsSection can display all or a subset of the objects you have for any given entity. SCObjectSection is used to display the actual attributes for a specific object. To set any section header in Sensible TableView, just use either headerTitle or headerView properties of SCTableViewSection (the superclass for SCArrayOfObjectsSection and SCObjectSection).

If you're still stuck, then please provide me with a screenshot of what you're trying to achieve and I'll post some sample code for you. Thanks!
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72

Re: Tableview section headers 1 year, 8 months ago #3

I guess that's my problem, I can get section header title for my core data entity, but I was only able to get single non-dynamic header title using for example following code:

[SCArrayOfObjectsSection sectionWithHeaderTitle:@"Tasks Section"
withItems:tasksArray withClassDefinition:taskClassDef];

What I would like have is something like this. Dynamically updating section headers...
screen_capture_2.png
  • jsaarinen
  • OFFLINE
  • Fresh Boarder
  • Posts: 11
  • Karma: 0

Re: Tableview section headers 1 year, 8 months ago #4

You can dynamically change the section's header title at anytime from within your view controller using this code:

 
...
SCTableViewSection *section = [tableViewModel sectionAtIndex:mySectionIndex];
section.headerTitle = myHeaderTitle;
...
// modify other sections here
...
[tableViewModel.modeledTableView reloadData]; // You can also use reloadSections:withRowAnimation: here
...
 
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72

Re: Tableview section headers 1 year, 8 months ago #5

Could you open this bit more, is there delegate method that I could use for the section changes? Is that triggered when SCTableView is initially rendered? It would be nice to have some accessory method in my entity class which would provide strings for section header title and when SCTableView is drawn it would call this method and place items into correct section accordingly.
Sounds confusing, I know I just don't get it how core data items end up into correct section if I fetch whole entity to SCTableView.
BR/jsaarinen
  • jsaarinen
  • OFFLINE
  • Fresh Boarder
  • Posts: 11
  • Karma: 0

Re: Tableview section headers 1 year, 8 months ago #6

Ok I think I now understand what you want You have an SCArrayOfObjectsSection, and you need to have each detail section get its header dynamically from a certain attribute of the managed object it's bound to. Here is some code on how to achieve that:

 
// Please remember to conform to SCTableViewModelDelegate in the header file for this method to get called
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
detailViewWillAppearForSectionAtIndex:(NSUInteger)index
withDetailTableViewModel:(SCTableViewModel *)detailTableViewModel
{
// First get the detail section
SCTableViewSection *detailSection = [detailTableViewModel sectionAtIndex:0];
 
// Let's say you have an attribute in your detail section bound object called "sectionTitle"
NSString *sectionTitle = [detailSection.boundObject valueForKey:@"sectionTitle"];
 
// Now set the header title
detailSection.headerTitle = sectionTitle;
}
 


Hope this helps
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72
  • Page:
  • 1
  • 2
Time to create page: 0.96 seconds