4

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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
EMontesMac
  • 123
  • 1
  • 2
  • 10

2 Answers2

3
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    if ([aTableView numberOfRowsInSection:section] == 0) return nil;

    return [alphabet objectAtIndex:section];
}

UITableView Class Reference

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
1

Thank you so much Evan, I thought to do that would be very complicated, but it wasn't! I have taken your code and adapted to mine:

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {

    if ([[facilitiesDictionary objectAtIndex:section] count]==0) 
        return nil;

    return [alphabet objectAtIndex:section];
}
EMontesMac
  • 123
  • 1
  • 2
  • 10
  • this should be selected as the proper answer. The one provided by Evan actually makes the app crash. You need to look at your array of objects to determine if there's nothing for that section. – roocell Dec 18 '11 at 21:53
  • 3
    @roocell: Just because the code I provided crashes when you copy and paste it into your project doesn't constitute a downvote. I simply gave relevant code to help the OP. Questions should rarely need to be answered with complete code - snippets and samples are generally used. If the OP, or even you, found that the code broke due to an array indexing issue, a comment should be left on my answer, which I would have promptly responded to. – Evan Mulawski Jan 17 '12 at 02:05