兩步搞定索引

索引看似復(fù)雜,其實(shí)是個(gè)很簡單的東西,可以說是tableView本身自帶的技能,只需要我們把他的任督二脈打通就行了。

實(shí)現(xiàn)索引只需要兩步:

  1. 創(chuàng)建索引的數(shù)據(jù)源
  2. 實(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容