Day.03.03 UITableView 表视图索引


#import "ViewController.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (nonatomic,strong)NSDictionary *plist;

@property (nonatomic,strong)NSArray *allkeys;

@property (nonatomic,strong)NSMutableArray *datalist;

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor greenColor];
    
    
    /*_______________________________________________________________*/
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"ListData" ofType:@"plist"];
    
    //plist文件中的字典
    _plist = [NSDictionary dictionaryWithContentsOfFile:path];
    
    //所有key的数组
    _allkeys = [_plist allKeys];
    
    //排序 A --> G
    
    /**
     *  系统的方法: 通过comepare方法实现数组重新排序
     
                    compare:  升序排序数组
     */
    _allkeys = [_allkeys sortedArrayUsingSelector:@selector(compare:)];
    
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    
    /*_______________________________________________________________*/
    
}

#pragma mark --UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return _plist.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *key = _allkeys[section];
    
    NSArray *subArray = [_plist objectForKey:key];
    
    return subArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identy = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
    }
    
    NSString *key = _allkeys[indexPath.section];
    
    NSArray *subArray = [_plist objectForKey:key];
    
    cell.textLabel.text = [subArray objectAtIndex:indexPath.row];
    
    return cell;
    
}

//返回 右侧索引兰的标题数组 --> 如果实现的话 会生成一个分组索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return _allkeys;
    
}

//点击右侧 组标题索引视图的时候调用
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    
    //title --> 组标题 index --> 组下标
    NSLog(@"%@  %ld",title,index);
    
    return index;
}

#pragma mark --uitableviewdelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    label.backgroundColor = [UIColor cyanColor];
//    label.text = [NSString stringWithFormat:@"%ld",section];
    
    label.text = _allkeys[section];
    
    return label;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 50;

}
@end

Day.03.03 UITableView 表视图索引_第1张图片
屏幕快照 2016-03-03 下午8.17.53.png
Day.03.03 UITableView 表视图索引_第2张图片
屏幕快照 2016-03-03 下午8.17.37.png

你可能感兴趣的:(Day.03.03 UITableView 表视图索引)