黄锦的移动开发博客

专注Iphone,Android开发

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

实际上UITableView默认就支持象电话本那样的按首字母索引。 实现sectionIndexTitlesForTableView 和 sectionForSectionIndexTitle 这两个接口即可。 细节请参考UITableViewDataSource帮助文档。

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{

     NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];

     for(char c = ‘A’;c<=‘Z’;c++)

          [toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];

    return toBeReturned;

}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{

    NSInteger count = 0;

     for(NSString *character in arrayOfCharacters)

   {

        if([character isEqualToString:title])

        {

                 return count;

          }

         count ++;

    }

     return 0;

}

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

      if([arrayOfCharacters count]==0)

  {

         return @”";

    }

    return [arrayOfCharacters objectAtIndex:section];

}

posted on 2010-09-26 13:20  黄锦  阅读(3155)  评论(1编辑  收藏  举报