UITableView-字母 [英] UITableView - Alphabet

查看:107
本文介绍了UITableView-字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我希望添加到带有字母的UITableView中的改进:

Here is the improvement that I wish to add to my UITableView with alphabet:

如果我的表中没有没有以以下其中一个开头的结果字母,我不想在UITableView中看到此titleForHeaderInSection。

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.

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

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:

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:

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


推荐答案

非常感谢Evan,我认为这样做非常复杂,但事实并非如此!我已经采用了您的代码并适应了我的要求:

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

这篇关于UITableView-字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆