兩步搞定索引

索引看似復(fù)雜嫂伞,其實(shí)是個(gè)很簡(jiǎn)單的東西今膊,可以說是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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末肢扯,一起剝皮案震驚了整個(gè)濱河市妒茬,隨后出現(xiàn)的幾起案子担锤,更是在濱河造成了極大的恐慌蔚晨,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肛循,死亡現(xiàn)場(chǎng)離奇詭異铭腕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)多糠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門累舷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人夹孔,你說我怎么就攤上這事被盈∥瞿酰” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵只怎,是天一觀的道長(zhǎng)袜瞬。 經(jīng)常有香客問我,道長(zhǎng)身堡,這世上最難降的妖魔是什么邓尤? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮贴谎,結(jié)果婚禮上汞扎,老公的妹妹穿的比我還像新娘。我一直安慰自己擅这,他們只是感情好澈魄,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蕾哟,像睡著了一般一忱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上谭确,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天帘营,我揣著相機(jī)與錄音,去河邊找鬼逐哈。 笑死芬迄,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的昂秃。 我是一名探鬼主播禀梳,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼肠骆!你這毒婦竟也來了算途?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤蚀腿,失蹤者是張志新(化名)和其女友劉穎嘴瓤,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體莉钙,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡廓脆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了磁玉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片停忿。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蚊伞,靈堂內(nèi)的尸體忽然破棺而出席赂,到底是詐尸還是另有隱情吮铭,我是刑警寧澤,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布颅停,位于F島的核電站沐兵,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏便监。R本人自食惡果不足惜扎谎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望烧董。 院中可真熱鬧毁靶,春花似錦、人聲如沸逊移。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)胳泉。三九已至拐叉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間扇商,已是汗流浹背凤瘦。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留案铺,地道東北人蔬芥。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像控汉,于是被迫代替她去往敵國(guó)和親笔诵。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容