#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@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 表视图索引
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...