UITableView 索引詳細

系統(tǒng)默認

索引初認 主要實現(xiàn)的方法

  • 索引UI
// 每個分區(qū)的頁眉
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [sectionTitles objectAtIndex:section];
}
// 索引目錄
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return sectionTitles;
}
  • 點擊索引砰盐,跳轉到點擊的分區(qū)
// 點擊目錄
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    // 獲取所點目錄對應的indexPath值
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
    
    // 讓table滾動到對應的indexPath位置
    [tableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    return index;
}

設置導航字體顏色、字體坑律、背景色(常用)

     for (UIView* subview in [self.tableView subviews]) 
     {
            if ([subview isKindOfClass:NSClassFromString(@"UITableViewIndex")])
            {
                   if([subview respondsToSelector:@selector(setIndexColor:)])
                   {
                            [subview performSelector:@selector(setIndexColor:) withObject:[UIColor redColor]];
                   }
                   if([subview respondsToSelector:@selector(setFont:)])
                   {
                          [subview performSelector:@selector(setFont:) withObject:[UIFont systemFontOfSize:14]];
                   }

                   if([subview respondsToSelector:@selector(setBackgroundColor:)])
                   {
                          [subview performSelector:@selector(setBackgroundColor:) withObject:[UIColor redColor]];
                   }

        }

實際效果

自定義索引

方法封裝 (參考UITableView+NBA)

  • UITableView+LCL.h
//
//  UITableView+LCL.h
//  tableview
//
//  Created by bloodspasm on 2016/08/11.
//  Copyright ? 2016年 bloodspasm. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UITableView (LCL)
- (UIView *)sectionIndexView;
- (void)setSectionIndexBackgroundColor:(UIColor *)BackgroundColor;
- (void)setSectionIndexTextColor:(UIColor *)textColor;
- (void)setSectionIndexFont:(UIFont *)font;
- (void)setSectionIndexFont:(UIFont *)font textColor:(UIColor *)textColor;
@end
  • UITableView+LCL.m
//
//  UITableView+LCL.m
//  tableview
//
//  Created by bloodspasm on 2016/08/11.
//  Copyright ? 2016年 bloodspasm. All rights reserved.
//

#import "UITableView+LCL.h"

@implementation UITableView (LCL)

- (UIView *)sectionIndexView {
    for (UIView *view in self.subviews) {
        if ([view respondsToSelector:@selector(setIndexColor:)]) {
            return view;
        }
    }
    return nil;
}

- (void)setSectionIndexBackgroundColor:(UIColor *)BackgroundColor{
    UIView *sectionIndexView = [self sectionIndexView];
    if (sectionIndexView) {
        if ([sectionIndexView respondsToSelector:@selector(setBackgroundColor:)]) {
            [sectionIndexView performSelector:@selector(setBackgroundColor:) withObject:BackgroundColor];
        }
    }
}

- (void)setSectionIndexTextColor:(UIColor *)textColor {
    UIView *sectionIndexView = [self sectionIndexView];
    if (sectionIndexView) {
        if ([sectionIndexView respondsToSelector:@selector(setIndexColor:)]) {
            [sectionIndexView performSelector:@selector(setIndexColor:) withObject:textColor];
        }
    }
}

- (void)setSectionIndexFont:(UIFont *)font {
    UIView *sectionIndexView = [self sectionIndexView];
    if (sectionIndexView) {
        if ([sectionIndexView respondsToSelector:@selector(setFont:)]) {
            [sectionIndexView performSelector:@selector(setFont:) withObject:font];
        }
    }
}

- (void)setSectionIndexFont:(UIFont *)font textColor:(UIColor *)textColor {
    UIView *sectionIndexView = [self sectionIndexView];
    if (sectionIndexView) {
        if ([sectionIndexView respondsToSelector:@selector(setIndexColor:)]) {
            [sectionIndexView performSelector:@selector(setIndexColor:) withObject:textColor];
        }
        if ([sectionIndexView respondsToSelector:@selector(setFont:)]) {
            [sectionIndexView performSelector:@selector(setFont:) withObject:font];
        }
    }
}
@end
  • 使用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = contentsArray[indexPath.section][indexPath.row];
    [tableView  setSectionIndexFont:[UIFont systemFontOfSize:25] textColor:[UIColor blueColor]];
    [tableView setSectionIndexBackgroundColor:[UIColor colorWithWhite:.5 alpha:1]];
    return cell;

}

資料

Demo:IndexTitlesForTableView

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末岩梳,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子晃择,更是在濱河造成了極大的恐慌冀值,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宫屠,死亡現(xiàn)場離奇詭異列疗,居然都是意外死亡,警方通過查閱死者的電腦和手機浪蹂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門抵栈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人坤次,你說我怎么就攤上這事古劲。” “怎么了缰猴?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵产艾,是天一觀的道長。 經(jīng)常有香客問我滑绒,道長闷堡,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任疑故,我火速辦了婚禮杠览,結果婚禮上,老公的妹妹穿的比我還像新娘纵势。我一直安慰自己倦零,他們只是感情好误续,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著扫茅,像睡著了一般蹋嵌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上葫隙,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天栽烂,我揣著相機與錄音,去河邊找鬼恋脚。 笑死腺办,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的糟描。 我是一名探鬼主播怀喉,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼船响!你這毒婦竟也來了躬拢?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤见间,失蹤者是張志新(化名)和其女友劉穎聊闯,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體米诉,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡菱蔬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了史侣。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拴泌。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖惊橱,靈堂內(nèi)的尸體忽然破棺而出蚪腐,到底是詐尸還是另有隱情,我是刑警寧澤李皇,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站宙枷,受9級特大地震影響掉房,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜慰丛,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一卓囚、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧诅病,春花似錦哪亿、人聲如沸粥烁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽讨阻。三九已至,卻和暖如春篡殷,著一層夾襖步出監(jiān)牢的瞬間钝吮,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工板辽, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留奇瘦,地道東北人。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓劲弦,卻偏偏與公主長得像耳标,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子邑跪,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

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

  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫次坡、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,121評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,304評論 25 707
  • 《梁思成·林徽因》講述了“大時代背景下跌宕起伏的個人命運”:一對著名夫婦的人生軌跡和中國近現(xiàn)代的激蕩史密切交織呀袱。這...
    簡書視覺館小飛俠閱讀 1,868評論 0 19
  • ?批評是一種很強大的負面能量贸毕,也是最常見的語言暴力。比如很多人總是會習慣性地說:“唉呀夜赵,怎么那么不小心明棍,起來”、“...
    博韜說閱讀 426評論 0 1
  • 小新大概是兩歲不到的樣子寇僧,他在桌子的角落里發(fā)現(xiàn)了一支圓珠筆摊腋,他玩那支筆,不停的戳時嘁傀,發(fā)現(xiàn)筆可以有點點出來兴蒸。 就這樣...
    米粒2020閱讀 511評論 0 1