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

test


  1. Weight Loss. Green tea increases the metabolism. The polyphenol found in green tea works to intensify levels of fat oxidation and the rate at which your body turns food into calories.
  2. Diabetes. Green tea apparently helps regulate glucose levels slowing the rise of blood sugar after eating. This can prevent high insulin spikes and resulting fat storage.
  3. Heart Disease. Scientists think, green tea works on the lining of blood vessels, helping keep them stay relaxed and better able to withstand changes in blood pressure. It may also protect against the formation of clots, which are the primary cause of heart attacks.
  4. Esophageal Cancer. It can reduce the risk of esophageal cancer, but it is also widely thought to kill cancer cells in general without damaging the healthy tissue around them.
  5. Cholesterol. Green tea reduces bad cholesterol in the blood and improves the ratio of good cholesterol to bad cholesterol.
UILabel *mLabel=[[UILabel alloc]init];
 mLabel.frame=CGRectMake(50, 100, 220, 100);
 mLabel.text=@"Hello world";
 [self.view addSubview:mLabel];
UILabel *mLabel=[[UILabel alloc]init]; mLabel.frame=CGRectMake(50, 100, 220, 100); mLabel.text=@"Hello world"; [self.view addSubview:mLabel]
[[NSNotificationCenter defaultCenter] addObserverForName:kMessagingSessionConnected
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue]
                                                  usingBlock:^(NSNotification * __unused notification) {

                                                      [self publishConnectionStatus:notification.userInfo[@"reason"]];


  [[UINavigationBar appearance] setBarTintColor:[UIColor navigationBarColor]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 

Test 10



The genus Actinidia contains around 60 species. Though most kiwifruit are easily recognized as kiwifruit (due to basic shape) their fruit is quite variable. The skin of the fruit can vary in size, shape, hairiness, and color. The flesh can vary in color, juiciness, texture, and taste. Some fruits are unpalatable while others taste considerably better than the majority of the commercial varieties.[1][10]
The most common kiwifruit is the fuzzy kiwifruit, from the species A. deliciosa. Other species that are commonly eaten include golden kiwifruit (A. chinensis), Chinese egg gooseberry (A. coriacea), baby kiwifruit (A. arguta), Arctic kiwifruit (A. kolomikta), red kiwifruit (A. melanandra), silver vine (A. polygama), purple kiwifruit (A. purpurea).[10]
The genus Actinidia contains around 60 species. Though most kiwifruit are easily recognized as kiwifruit (due to basic shape) their fruit is quite variable. The skin of the fruit can vary in size, shape, hairiness, and color. The flesh can vary in color, juiciness, texture, and taste. Some fruits are unpalatable while others taste considerably better than the majority of the commercial varieties.[1][10]
The most common kiwifruit is the fuzzy kiwifruit, from the species A. deliciosa. Other species that are commonly eaten include golden kiwifruit (A. chinensis), Chinese egg gooseberry (A. coriacea), baby kiwifruit (A. arguta), Arctic kiwifruit (A. kolomikta), red kiwifruit (A. melanandra), silver vine (A. polygama), purple kiwifruit (A. purpurea).[10]
The genus Actinidia contains around 60 species. Though most kiwifruit are easily recognized as kiwifruit (due to basic shape) their fruit is quite variable. The skin of the fruit can vary in size, shape, hairiness, and color. The flesh can vary in color, juiciness, texture, and taste. Some fruits are unpalatable while others taste considerably better than the majority of the commercial varieties.[1][10]
The most common kiwifruit is the fuzzy kiwifruit, from the species A. deliciosa. Other species that are commonly eaten include golden kiwifruit (A. chinensis), Chinese egg gooseberry (A. coriacea), baby kiwifruit (A. arguta), Arctic kiwifruit (A. kolomikta), red kiwifruit (A. melanandra), silver vine (A. polygama), purple kiwifruit (A. purpurea).[10]

Test 9



The genus Actinidia contains around 60 species. Though most kiwifruit are easily recognized as kiwifruit (due to basic shape) their fruit is quite variable. The skin of the fruit can vary in size, shape, hairiness, and color. The flesh can vary in color, juiciness, texture, and taste. Some fruits are unpalatable while others taste considerably better than the majority of the commercial varieties.[1][10]
The most common kiwifruit is the fuzzy kiwifruit, from the species A. deliciosa. Other species that are commonly eaten include golden kiwifruit (A. chinensis), Chinese egg gooseberry (A. coriacea), baby kiwifruit (A. arguta), Arctic kiwifruit (A. kolomikta), red kiwifruit (A. melanandra), silver vine (A. polygama), purple kiwifruit (A. purpurea).[10]
The genus Actinidia contains around 60 species. Though most kiwifruit are easily recognized as kiwifruit (due to basic shape) their fruit is quite variable. The skin of the fruit can vary in size, shape, hairiness, and color. The flesh can vary in color, juiciness, texture, and taste. Some fruits are unpalatable while others taste considerably better than the majority of the commercial varieties.[1][10]
The most common kiwifruit is the fuzzy kiwifruit, from the species A. deliciosa. Other species that are commonly eaten include golden kiwifruit (A. chinensis), Chinese egg gooseberry (A. coriacea), baby kiwifruit (A. arguta), Arctic kiwifruit (A. kolomikta), red kiwifruit (A. melanandra), silver vine (A. polygama), purple kiwifruit (A. purpurea).[10]
The genus Actinidia contains around 60 species. Though most kiwifruit are easily recognized as kiwifruit (due to basic shape) their fruit is quite variable. The skin of the fruit can vary in size, shape, hairiness, and color. The flesh can vary in color, juiciness, texture, and taste. Some fruits are unpalatable while others taste considerably better than the majority of the commercial varieties.[1][10]
The most common kiwifruit is the fuzzy kiwifruit, from the species A. deliciosa. Other species that are commonly eaten include golden kiwifruit (A. chinensis), Chinese egg gooseberry (A. coriacea), baby kiwifruit (A. arguta), Arctic kiwifruit (A. kolomikta), red kiwifruit (A. melanandra), silver vine (A. polygama), purple kiwifruit (A. purpurea).[10]