表视图中的侧索引
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view, typically from a nib.
? ? //創(chuàng)建tableview
? ? UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
? ? tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
? ? tableView.dataSource = self;
? ? tableView.delegate = self;
? ? [self.view addSubview:tableView];
? ? //設(shè)置索引字體的顏色
? ? tableView.sectionIndexColor = [UIColor greenColor];
? ? //設(shè)置索引背景顏色
? ? tableView.sectionIndexBackgroundColor = [UIColor redColor];
? ? //設(shè)置點(diǎn)擊后索引背景的顏色
? ? tableView.sectionIndexTrackingBackgroundColor = [UIColor blackColor];
? ? //獲取文件路徑
? ? NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ListData" ofType:@"plist"];
? ? self.dataDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
? ? allKeys = [self.dataDic allKeys];
?? ?
? ? //排序
? ? allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
}
- (void)didReceiveMemoryWarning {
? ? [super didReceiveMemoryWarning];
? ? // Dispose of any resources that can be recreated.
}
//返回組數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
? ? return allKeys.count;
}
//返回每組的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
? ? NSArray *cellArray =[self.dataDic objectForKey:allKeys[section]] ;
? ? return cellArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
? ? NSString *identifier = @"Cell";
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
? ? if (cell == nil) {
? ? ? ? cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
? ? }
? ? NSArray *cellArray =[self.dataDic objectForKey:allKeys[indexPath.section]] ;
? ? cell.textLabel.text = [cellArray objectAtIndex:indexPath.row];
? ? return cell;
}
//section標(biāo)題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
? ? return allKeys[section];
}
//索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
? ? return allKeys;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
? ? return index;
}
轉(zhuǎn)載于:https://blog.51cto.com/10594302/1699719
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
- 上一篇: Android——发送和接收广播
- 下一篇: 测试杯子