UITableView - Alphabet

孤街醉人 提交于 2019-12-10 09:24:57

问题


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

回答1:


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

    return [alphabet objectAtIndex:section];
}

UITableView Class Reference




回答2:


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];
}


来源:https://stackoverflow.com/questions/4286744/uitableview-alphabet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!