NSFetchedResultsController with indexed UITableViewController and UILocalizedIndexedCollation - objective-c

I have a table that uses an NSFetchedResultsController. This gets me an index with the headers that are present
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[_fetchedResultsController sections] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[_fetchedResultsController sections] objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numberOfRows = 0;
NSArray *sections = _fetchedResultsController.sections;
if(sections.count > 0)
{
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];
numberOfRows = [sectionInfo numberOfObjects];
}
return numberOfRows;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [_fetchedResultsController sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
but I want to use UILocalizedIndexCollation to get the complete alphabet.
How do I wire up these methods to the NSFetchedResultsController?
I think I need to get the index titles from the current Collation
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
but I am lost on how to write this method
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
//How do I translate index here to be the index of the _fetchedResultsController?
return [_fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

I think you have to traverse the fetched results controller sections and find a matching section for the given title, for example:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSInteger section = 0;
for (id <NSFetchedResultsSectionInfo> sectionInfo in [_fetchedResultsController sections]) {
if ([sectionInfo.indexTitle compare:title] >= 0)
break;
section++;
}
return section;
}
For section index titles that to not have a matching section, you have to decide if you want to jump to a "lower" or "higher" section. The above method jumps to the next higher section.

The solution I went with, after inspiration from Martin.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSInteger localizedIndex = [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
NSArray *localizedIndexTitles = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
for(int currentLocalizedIndex = localizedIndex; currentLocalizedIndex > 0; currentLocalizedIndex--) {
for(int frcIndex = 0; frcIndex < [[_fetchedResultsController sections] count]; frcIndex++) {
id<NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:frcIndex];
NSString *indexTitle = sectionInfo.indexTitle;
if([indexTitle isEqualToString:[localizedIndexTitles objectAtIndex:currentLocalizedIndex]]) {
return frcIndex;
}
}
}
return 0;
}

Related

UISearchController – Search result show different records

When i type "stefanik" in searchbar the result is one record (but different look), when i tap on this, is the right record, see screenshots.
I think problem is in these methods, but i cant find the problem.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.searchController.active)
{
return 1;
}
else
{
return [[self.fetchedResultsController sections] count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchController.active)
{
return [self.filteredList count];
}
else
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
}
Have you seen this problem before?
Screenshots
If you are using NSFetchedResultsController apply an NSPredicate to filter the records for example
if (self.searchController.active) {
self.fetchedResultsController.fetchRequest.predicate = [NSPredicate predicateWithBlock: ^BOOL(id obj, NSDictionary *bind){
return [[(Airport *)obj name] rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound;
}];
} else {
self.fetchedResultsController.fetchRequest.predicate = nil
}
// reload data
This is more efficient than filtering in code into an extra array

Insert Cells and Sections from Table Views

Help me ! I can not able to insert cells in section tableviews !
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger result = 0;
result = [[_dictionoryOfDay allKeys] count];
return result;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger result = 0;
NSString *sectionNameInDictionary = [[_dictionoryOfDay allKeys]
objectAtIndex:section];
NSArray *sectionArray = [_dictionoryOfDay objectForKey:
sectionNameInDictionary];
result = [sectionArray count];
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UICustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellMain" forIndexPath:indexPath];
[cell.btnAddMore addTarget:self action:#selector(actionAddMore:)forControlEvents:UIControlEventTouchUpInside];
cell.btnAddMore.tag = indexPath.section;
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
TableHeaderAvailabel *header;
if(!header){
header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"TableHeaderAvailabel"];
}
NSString *result = nil;
result = [[self.dictionoryOfDay allKeys] objectAtIndex:section];
header.lbTitle.text = result;
return header;
}
-(void)actionAddMore:(UIButton *)sender{
}
My guess is that you want to add a row to your section when you tap on a cell's btnAddMore. If that's the case, then the implementation of actionAddMore() should look like
NSString *sectionNameInDictionary = [[_dictionoryOfDay allKeys]
objectAtIndex:section];
NSArray *sectionArray = [_dictionoryOfDay objectForKey:
sectionNameInDictionary];
// Create whatever you use to populate rows
[sectionArray addObject: <your new object>];
_dictionaryOfDay[sectionNameInDictionary] = sectionArray;
[self.tableView reloadData]; // Or just load the new cell, but this is easier

How to create sections for parsed Data in objective c

I have parsed data which I put in tableview alphabetically. Alphabetical order is based on the last name of a person. Now I need to add index bar and sections. What I am creating is kind of address book. This is the code I am using to put the parsed data in the cell label.text = [arrayOfTitle objectAtIndex:indexPath.row];
This is the code through which I placed index bar and section titles on the tableview:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self arrayOfNames] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 26;
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
if ([self.arrayOfNames objectAtIndex:section] ) {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]
objectAtIndex:section];
}
return nil;
}
#end
I know how to create sections out of plist file but since here I just have an array of parsed data, I am having a hard time to break it into sections.
Thank you for your help.

Remove sections with no rows from UITableView

I would like to remove section headers from a UITableView if there are no rows for that section.
I'm using UILocalizedIndexedCollation for my section headers. So when I create the headers, I don't necessarily know what sections will have content.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
//return [customerSections count];
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
}
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//NSLog(#"Section: %i", section);
if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.filteredCustomers.count;
} else {
return [[self.customerData objectAtIndex:section] count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;//#"Results";
}
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//return [customerSections allKeys];
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;
}
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
Just wanted to chime in and give my solution to this
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if ([self.myTableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
return nil;
}
return [[self.collation sectionTitles] objectAtIndex:section];
}
based on this answer
I've recently achieved this with this code:
This is the function that returns the title for the section, if there are no rows in that section then don't return a title:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if ([self.customerData count] > 0 ) {
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
return nil;
}
It's an interesting question, with a number of possible solutions.
Working backwards, numberOfSectionsinTableView and numberOfRowsInSection are what need to be updated in order to display the right number of sections. These are partly dependent on the methods UILocalizedIndexedCollation methods.
(Presumably this happens after some sort of user action (a delete or an insert), so note that in commitEditingStyle you should call [self.tableView reloadData).
I am assuming that customerData is an array, where at each index there is a mutable array that corresponds to a section. When the array at a certain customerData index has no data, you want to remove the section for that index.
The solution then, is to calculate everything manually - determine section information based on lives inside your customerData array.
I took a stab at rewriting three of your methods. Good luck!
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return nil;
}
NSMutableArray *arrayToFilter = [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
//This is the key - recalculate index titles based on what's present in customerData
for (int i = [self.customerData count] -1; i >=0 ; i--) {
if (![[self.customerData objectAtIndex:i] count]) {
[self.arrayToFilter removeObjectAtIndex:i];
}
}
return arrayToFilter;
}
//You need to be calculating your table properties based on the number of objects
//rather of the 'alphabet' being used.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
//return [customerSections count];
if (tableView == self.searchDisplayController.searchResultsTableView) {
return 1;
}
//return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
int populatedArrayCounter = 0;
for (int i = 0; i <[self.customerData count]; i++) {
if ([[self.customerData objectAtIndex:i] count]) {
populatedArrayCounter++;
}
}
return populatedArrayCounter;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//NSLog(#"Section: %i", section);
if (tableView == self.searchDisplayController.searchResultsTableView) {
return self.filteredCustomers.count;
} else {
// Your original line requires changing, because there are customerData array objects with a count of 0, and you don't want any sections like that
// Thus, pick from the set of populated arrays.
NSMutableArray populatedArrays = [[NSMutableArray alloc] init];
for (int i = 0; i <[self.customerData count]; i++) {
if ([[self.customerData objectAtIndex:i] count]) {
[populatedArrays addObject:i];
}
}
return [[populatedArrays objectAtIndex:section] count];;
}
}
I wanted to share my solution in swift
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if self.sections[section].isEmpty
{
return nil
}
else
{
return collation.sectionTitles[section]
}
}
I ended up removing the unused sectionIndexTitles and creating the sections based on that.
In my NSURLConnection requestDidFinish I used the following.
self.customerData = [self partitionObjects:[self customers] collationStringSelector:#selector(self)];
then had
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
sectionIndexTitles = [NSMutableArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count];
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
for (int i = 0; i < sectionCount; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
for (id object in array) {
NSInteger index = [collation sectionForObject:[object objectForKey:#"name"] collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray array];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSUInteger lastIndex = 0;
NSMutableIndexSet *sectionsToRemove = [NSMutableIndexSet indexSet];
for (NSArray *section in unsortedSections) {
if ([section count] == 0) {
NSRange range = NSMakeRange(lastIndex, [unsortedSections count] - lastIndex);
[sectionsToRemove addIndex:[unsortedSections indexOfObject:section inRange:range]];
lastIndex = [sectionsToRemove lastIndex] + 1;
} else {
NSArray *sortedArray = [section sortedArrayUsingDescriptors:sortDescriptors];
[sections addObject:sortedArray];
}
}
[sectionIndexTitles removeObjectsAtIndexes:sectionsToRemove];
return sections;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (self.searchDisplayController.active) {
return 1;
}
return [sectionIndexTitles count];//[[[UILocalizedIndexedCollation currentCollation] sectionTitles] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.searchDisplayController.active) {
return self.filteredCustomers.count;
} else {
return [[self.customerData objectAtIndex:section] count];
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (self.searchDisplayController.active) {
return nil;
}
return [sectionIndexTitles objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (self.searchDisplayController.active) {
return nil;
}
return sectionIndexTitles;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;//[[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
And with all the above code this removed the unused letters from the right side of the screen as well as the section headers that had no rows.

shouldReloadTableForSearchString doesn't show filtered result

I have been trying for hours but couldn't figure out why it doesn't show the filtered result. It always shows all the results. I'm not sure what I am doing wrong.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
/*
Update the filtered array based on the search text and scope.
*/
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
//for loop here
NSLog(#"%i", [filteredListContent count]);
//filteredListContent contains correct number of filtered items
}
It works. I wasn't using the filtered result array in numberOfRowsInSection method
was:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.persons count];
}
changed to:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.filteredListContent count];
}
else
{
return [self.persons count];
}
}