How To Add Index List on Table View In iOS App....

How To Add Index List on Table View In iOS App....

Hello friends, I hope you all are enjoying your iOS programming learning here. In my previous post, I have provided tutorial for how to reorder table view cell by drag and drop on iOS  by putting the table view in editing mode. In case you don’t know how to reorder table view cell by drag and drop, I will suggest you to read my previous post here. 


Today in this tutorial, I will show you, how to add index list on tableview. This is a very useful feature which you may need to implement in most of your iOS app.

At the end of this tutorial, your app will look something like the following image.



First create a Xcode project. Select "Single view application" and tap the next button (Please follow the screen shots).


Give a name to your project .....


Tap next button and then tap the create button in next page to create your new Xcode project. Now go to Storyboard in your project and find UITableView object. Drag drop a UITableView object  on your ViewController scene and let it cover the entire UIViewController Scene. 


Now create an reference outlet for your UITableView object in your ViewController.h file and name it as mTableView.  (Please follow the screen shots).



Now select your ViewController.h file  and Append “<UITableViewDelegate, UITableViewDataSource>” after “UIViewController”. Declare two NSArray. One is for showing the section title and another is for populating the tableView.  

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *sectionDataArray;
    NSArray *tableDataArray;
}
@property (weaknonatomicIBOutlet UITableView *mTableView;

@end

Now your ViewController.h file should look something like the following image.



select your ViewController.m file and under -(void)viewDidLoad method, write the following code to populate our tableDataArray and sectionDataArray and set the delegate and datasource of our UITableView object.




  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.      
  4.     tableDataArray = [[NSArray alloc]initWithObjects:  
  5.                       @"Ant",  
  6.                       @"African lion",  
  7.                       @"Bear",  
  8.                       @"Black Swan",  
  9.                       @"Buffalo",  
  10.                       @"Camel",  
  11.                       @"Cockatoo",  
  12.                       @"Dog",  
  13.                       @"Donkey",  
  14.                       @"Emu",  
  15.                       @"Giraffe",  
  16.                       @"Greater Rhea",  
  17.                       @"Hippopotamus",  
  18.                       @"Horse",  
  19.                       @"Koala",  
  20.                       @"Lion",  
  21.                       @"Llama",  
  22.                       @"Manatus",  
  23.                       @"Meerkat",  
  24.                       @"Panda",  
  25.                       @"Peacock",  
  26.                       @"Pig",  
  27.                       @"Platypus",  
  28.                       @"Polar Bear",  
  29.                       @"Rhinoceros",  
  30.                       @"Seagull",  
  31.                       @"Tasmania Devil",  
  32.                       @"Whale",  
  33.                       @"Whale Shark",  
  34.                       @"Wombat",  
  35.                       nil];  
  36.       
  37. sectionDataArray=[[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"K",@"L",@"M",@"P",@"R",@"S",@"T",@"W", nil];  
  38.       
  39.     self.mTableView.delegate=(id)self;  
  40.     self.mTableView.dataSource=(id)self;  
  41.       
  42. }  

Now its time to implement our UITableView Delegate and DataSource methods to populate data in tableView. We will use NSPredicate to find out which animal name comes under which alphabet. If you want to learn more about NSPredicate, you can read my this post


  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     return [sectionDataArray count];  
  4. }  
  5.   
  6. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  7. {  
  8.     return [sectionDataArray objectAtIndex:section];  
  9. }  
  10.   
  11. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  12. {  
  13.     NSPredicate *predicate =[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sectionDataArray objectAtIndex:section]];  
  14.       
  15.    NSArray *tempArray = [tableDataArray filteredArrayUsingPredicate:predicate];  
  16.      
  17.     return [tempArray count];  
  18. }  
  19.   
  20. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  21. {  
  22.     static NSString *cellIdentifier = @"cell";  
  23.       
  24.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];  
  25.       
  26.     if (cell == nil) {  
  27.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];  
  28.     }  
  29.     NSPredicate *predicate =[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sectionDataArray objectAtIndex:indexPath.section]];  
  30.       
  31.     NSArray *tempArray = [tableDataArray filteredArrayUsingPredicate:predicate];  
  32.     cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];  
  33.     return cell;  
  34. }  
Now run the project and see the data listing of your tableView. But you will not see any index list functionality on table view.
Now Its time implement the index list part of our project. Implement the following  method in your project. 
  1. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView  
  2. {  
  3.     return sectionDataArray;  
  4. }  

Share this

Related Posts

Latest
Previous
Next Post »