4

アルファベットで UITableView に追加したい改善点は次のとおりです。

テーブルにアルファベットの文字で始まらない結果がない場合、UITableView でこの titleForHeaderInSection を表示したくありません。

そして、私はそれを行う方法を見つけられません。

私の現在の実装と画像を例として見ることができます ( http://blog.joefusco.name/wp-content/uploads/2010/10/indexed-table.jpg ):

施設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

ファシリティ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
4

2 に答える 2

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

    return [alphabet objectAtIndex:section];
}

UITableView クラス リファレンス

于 2010-11-26T16:35:13.173 に答える
1

どうもありがとう、エヴァン、それはとても複雑だと思ったけど、そうじゃなかった!私はあなたのコードを取り、私のものに適応させました:

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

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

    return [alphabet objectAtIndex:section];
}
于 2010-11-29T10:09:11.963 に答える