首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView -字母表

UITableView -字母表
EN

Stack Overflow用户
提问于 2010-11-26 23:57:25
回答 2查看 8.4K关注 0票数 4

以下是我希望用字母表添加到我的UITableView中的改进:

如果我的表中没有不以字母表中的一个字母开头的结果,我不希望在我的UITableView中看到这个titleForHeaderInSection。

我找不到这样做的方法。

您可以看到我当前的实现和一个示例图像(http://blog.joefusco.name/wp-content/uploads/2010/10/indexed-table.jpg):

facilitiesViewController.h:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-29 18:09:12

非常感谢Evan,我原以为这样做会很复杂,但事实并非如此!我采用了你的代码,并根据我的代码进行了调整:

代码语言:javascript
复制
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {

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

    return [alphabet objectAtIndex:section];
}
票数 1
EN

Stack Overflow用户

发布于 2010-11-27 00:35:13

代码语言:javascript
复制
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
    if ([aTableView numberOfRowsInSection:section] == 0) return nil;

    return [alphabet objectAtIndex:section];
}

UITableView Class Reference

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4286744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档