Here is the improvement that I wish to add to my UITableView with alphabet:
If there are no results in my table that don't start with one of the letters of the alphabet, I don't want to see this titleForHeaderInSection in my UITableView.
And I don't find the way to do that.
You can see my current implementation and an image as example (http://blog.joefusco.name/wp-content/uploads/2010/10/indexed-table.jpg):
facilitiesViewController.h:
@interface FacilitiesViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, ...> {
IBOutlet UITableView *facilitiesTableView;
NSMutableArray *facilitiesDictionary;
NSArray *alphabet;
...
}
@property (nonatomic, retain) NSMutableArray *facilitiesDictionary;
@property (nonatomic, retain) NSArray *alphabet;
...
@end
facilitiesViewController.m:
@implementation FacilitiesViewController
...
- (void)viewDidLoad {
[super viewDidLoad];
alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",
@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
facilitiesDictionary = [[NSMutableArray alloc]init];
for (int i = 0; i < [alphabet count]; i++)
[facilitiesDictionary insertObject:[[NSMutableArray alloc] init] atIndex: i];
... }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [alphabet count]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[facilitiesDictionary objectAtIndex:section] count]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
return cell; }
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return alphabet; }
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [alphabet indexOfObject:title]; }
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
return [alphabet objectAtIndex:section]; }
@end
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
if ([aTableView numberOfRowsInSection:section] == 0) return nil;
return [alphabet objectAtIndex:section];
}