加入指数(IOS开发)

该指数是用来协助查询。

原则上:

- 索引的标题是不完全一样的标题显示;

- 指数应该具有一定的代表性,它可表示一组数据;

- 假设索引列表视图。在一般情况下不再使用扩展视图。

(easy指向)

会又一次到的数据源方法:

tableView: numberOfRowsInSection: ------获取某节的行数

tableView:cellForRowAtIndexPath: -------Cell数据的实现

numberofSectionInTableView: ----------获取节数

tableView:titleForHeaderInSection: --------节标题

sectionIndexTitlesForTableView: --------获取索引




@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"team_dictionary"
                                           ofType:@"plist"];
    
    // 获取属性列表文件里的所有数据
    self.dicData = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *tempList = [self.dicData allKeys];
    // 对key进行排序
    // selector为SEL类型,sortedArrayUsingSelector方法的參数必须是selector
    self.listGroupname = [tempList sortedArrayUsingSelector:@selector(compare:)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 重写数据源方法
- (NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 依照节索引从小组名数组中获得组名
    NSString *groupName = [self.listGroupname objectAtIndex:section];
    // 将组名作为key,从字典中取出球队数组集合
    // 由于存储的时候是哈希结构
    NSArray *listTeams = [self.dicData objectForKey:groupName];
    // 这一节一共同拥有多少球队
    return [listTeams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    // 获得选择的节 A组即第1节
    NSUInteger section = [indexPath section];
    // 获得选择节中选中的行索引 -- A组第1、第2.。

NSUInteger row = [indexPath row]; // 依照节索引从小组名数组中获得组名 -- A组 NSString *groupName = [self.listGroupname objectAtIndex:section]; // 将组名作为key,从字典中取出球队数组集合 NSArray *listTeams = [self.dicData objectForKey:groupName]; cell.textLabel.text = [listTeams objectAtIndex:row]; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 该节行数 return [self.listGroupname count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { // 该节的节头 NSString *groupName = [self.listGroupname objectAtIndex:section]; return groupName; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { // 加入索引 NSMutableArray *listTitles = [[NSMutableArray alloc] initWithCapacity:[self.listGroupname count]]; // 把A组改为A for (NSString *item in self.listGroupname) { NSString *title = [item substringToIndex:1]; // 获取的第一个字符 [listTitles addObject: title]; // 字符串数组追加的最后位置 } return listTitles; } @end


注意:

在这个故事中要配置版本tableview委托协议。。!!

原文地址:https://www.cnblogs.com/lcchuguo/p/5040837.html