UITableView - Алфавит - PullRequest
       3

UITableView - Алфавит

4 голосов
/ 26 ноября 2010

Вот улучшение, которое я хочу добавить в свой UITableView с алфавитом:

Если в моей таблице нет результатов, которые не начинаются с одной из букв алфавита, я нехочу увидеть этот titleForHeaderInSection в моем UITableView.

И я не могу найти способ сделать это.

Вы можете увидеть мою текущую реализацию и изображение в качестве примера (http://blog.joefusco.name/wp-content/uploads/2010/10/indexed-table.jpg):

Facilities ViewController.h:

@interface FacilitiesViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, ...>  {
             IBOutlet UITableView     *facilitiesTableView;
             NSMutableArray           *facilitiesDictionary;
             NSArray                  *alphabet;
             ...
}
@property (nonatomic, retain)     NSMutableArray     *facilitiesDictionary;
@property (nonatomic, retain)     NSArray          *alphabet;
...

@end

Facilities ViewController.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

Ответы [ 2 ]

3 голосов
/ 26 ноября 2010
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    if ([aTableView numberOfRowsInSection:section] == 0) return nil;

    return [alphabet objectAtIndex:section];
}

Описание класса UITableView

1 голос
/ 29 ноября 2010

Большое спасибо, Эван, я думал, что это будет очень сложно, но это не так! Я взял твой код и приспособился к моему:

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

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

    return [alphabet objectAtIndex:section];
}
...