Tableview添加索引

//添加索引栏标题数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   
        NSMutableArray *resultArray =[NSMutableArray arrayWithObject:UITableViewIndexSearch];
        
        for (NSString *str in [[self.CityListArr[0] allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2){
            return [str1 compare:str2];
        }]) {
            
            [resultArray addObject:str];
        }
        
        NSLog(@"索引:%@",resultArray);
        return resultArray;

    
}


//点击索引栏标题时执行
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //这里是为了指定索引index对应的是哪个section的,默认的话直接返回index就好。其他需要定制的就针对性处理
    
        
        if ([title isEqualToString:UITableViewIndexSearch])
        {
            [tableView setContentOffset:CGPointZero animated:NO];//tabview移至顶部
            return NSNotFound;
        }
        else
        {
            return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index] - 1; // -1 添加了搜索标识
        }
    
    
}

你可能感兴趣的:(项目实用)