articles

Home / DeveloperSection / Articles / Implementing Sections in Country List in TableView

Implementing Sections in Country List in TableView

Tarun Kumar3391 25-Nov-2015

 Previous article: Implementing Country List in TableView

In our previous article we learn how to implement records from table from sqlite database in table view. Now in this article we will learn how to create sections in this table view, here we will continue where we finish our previous project.

In CountryListViewController.h file we will implement some other necessary table delegate methods like: titleForHeaderInSection: , sectionIndexTitlesForTableView: , didSelectRowAtIndexPath:

But, first we will create custom methods for fetching the titles, sorting the titles for every sections, storing these titles in a new array using NSPredicate class using the countryRecords array (we already learn about countryRecords variable in our previous articles) and other different types of codes we will use here , to do it follow the steps:

1.    Create a method fetchSectionTitle: and in the end of viewDidLoad: method in the CountryListViewController.m file, like this:

[self fetchSectionTitle];

fetchSectionTitle: method code like this: 

- (void)fetchSectionTitle
{
   NSMutableSet *set = [[NSMutableSet alloc]init];
   for(CountryListController *obj in countryRecords)
   {
        [set addObject:[[obj CountryName] substringToIndex:1]];
   }
   sectionTitles = [[set allObjects]
                            sortedArrayUsingSelector:
                            @selector(caseInsensitiveCompare:)];
}

fetchSectionTitle: method is used to fetch all the titles from the countryRecords array and add them in the variable of NSMutableSet in the sorted manner. 

2.  Now, in the numberOfSectionsInTableView: method return number of section titles, like this:

return [sectionTitles count];

3. Now, in the titleForHeaderInSection: method return the title for the specified section title of the table view from the sectionTitles array, like this:

return [sectionTitles objectAtIndex:section]; 

4. Now, in the numberOfRowsInSection: method return the number of rows in a given section of a table view (it is a required method). like this:

return [self GetCountryListFromSection:section].count;

 

But here, first we will create a custom method to get the list to display in the selected section, for this we are created a method GetCountryListFromSection: . It will return the total no of list in the sorted order for the selected section,

Here is the code of GetCountryListFromSection: method:

-(NSArray*)GetCountryListFromSection:(NSInteger)section
{
     NSPredicate * predicate = [NSPredicate predicateWithFormat:
                                                 @"CountryName BEGINSWITH[cd]%@",
                                                 [sectionTitles objectAtIndex:section]];
     return [countryRecords filteredArrayUsingPredicate:predicate]; }

Here we are using NSPredicate class which is used to define logical conditions used to constrain a search either for a fetch or for in memory filtering. After that we will return new array which contains the objects for which the predicate returns true.

5. Now, in the cellForRowAtIndexPath: method create objects of CountryListController class (this class is using for storing all the records from the table of sqlite database) like this:

CountryListController *countryObj=[self GetCountryObjectFromIndexPath:

     indexPath];

For this first we will create a custom method GetCountryObjectFromIndexPath: and its return type will be CountryListController class, here is the code:

-(CountryListController*)GetCountryObjectFromIndexPath:(NSIndexPath*)indexPath
{
    NSArray *countrySubList= [self GetCountryListFromSection:
                                             indexPath.section];
    return [countrySubList objectAtIndex:indexPath.row];

6. Now, in the sectionIndexTitlesForTableView: method, return the no of titles available in the sectionTitles array, it will display a list in the right-side of the screen, with this help we can navigate to that section which starts with the touched title of the screen, code like this:

return sectionTitles;

That’s it. Now you can Run your application: 

To see the changes in your application run the application by clicking ‘Run’ button of your Xcode, after that click on ‘Continue’ button, next click on ‘Search Country’ button, now our ‘CountryListViewController’ will be open with the country list with the section titles (for which we are currently implement the codes).

CountryListViewController will look like this: 


Implementing Sections in Country List in TableView


Updated 07-Sep-2019

Leave Comment

Comments

Liked By