索引看似復(fù)雜嫂伞,其實(shí)是個(gè)很簡(jiǎn)單的東西今膊,可以說是tableView本身自帶的技能审编,只需要我們把他的任督二脈打通就行了拗胜。
實(shí)現(xiàn)索引只需要兩步:
- 創(chuàng)建索引的數(shù)據(jù)源
- 實(shí)現(xiàn)代理方法
廢話不多說,直接上代碼檀训,代碼中有解釋
#import "ViewController.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *indexTableView;//要添加索引的tableView
@property (nonatomic, strong) NSMutableArray *dataSource;//索引數(shù)據(jù)源
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.indexTableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableView *)indexTableView {
if (!_indexTableView) {
_indexTableView = [[UITableView alloc] initWithFrame:self.view.frame];
_indexTableView.showsVerticalScrollIndicator = NO;
[_indexTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
_indexTableView.delegate = self;
_indexTableView.dataSource = self;
_indexTableView.sectionIndexColor = [UIColor blueColor];//給索引個(gè)顏色看看
// _indexTableView.sectionIndexBackgroundColor
// _indexTableView.sectionIndexTrackingBackgroundColor
}
return _indexTableView;
}
- (NSMutableArray *)dataSource {
//索引數(shù)據(jù)源
if (!_dataSource) {
_dataSource = [[NSMutableArray alloc] init];
for (char c = 'A'; c < 'Z'; c++) {
NSString *zimu = [NSString stringWithFormat:@"%c",c];
[_dataSource addObject:zimu];
}
}
return _dataSource;
}
#pragma mark - 添加索引列
//索引數(shù)據(jù)源
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//如果有多個(gè)tableView,這里需要做判斷處理柑潦,下面同理
return self.dataSource;
}
//索引列表點(diǎn)擊事件
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
//你要跳轉(zhuǎn)到哪一組?
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index + 4] atScrollPosition:UITableViewScrollPositionTop animated:YES];
return index + 4;
}
#pragma mark - table View//這就不多說了
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 30;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
headerView.backgroundColor = [UIColor redColor];
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
textLabel.text = [NSString stringWithFormat:@"%ld組",section];
[headerView addSubview:textLabel];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
return cell;
}
@end