iOS UITableView字母索引——系统自带检索栏

字母索引(UITableViewIndex)是tableView里使用的一个控件,继承自UIControl,但不是一个公开的类。
其主要作用是:当一个有较多分类的tableView的内容比较多的时候,提供了一个快速索引的功能,可以通过点击和滑动两种方式快速索引,并在iOS10及以后,增加了一个震动的反馈效果。

一、系统提供的参数和方法。(公开的)

/**
sectionIndexMinimumDisplayRowCount属性来指定当tableView中多少行的时候开始显示IndexList
*/
@property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount;                                                      // show special section index list on right when row count reaches this value. default is 0

/**
sectionIndexColor属性来设置索引列文本的颜色
*/
@property (nonatomic, strong, nullable) UIColor *sectionIndexColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;                   // color used for text of the section index

/**
sectionIndexBackgroundColor属性来设置索引列背景颜色
*/
@property (nonatomic, strong, nullable) UIColor *sectionIndexBackgroundColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;         // the background color of the section index while not being touched

/**
sectionIndexTrackingBackgroundColor属性来设置索引列被点击或滑动时的背景颜色
*/
@property (nonatomic, strong, nullable) UIColor *sectionIndexTrackingBackgroundColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while it is being touched
/**
通过返回section titles的数组,设置右侧索引
*/
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;                               // return list of section titles to display in section index view (e.g. "ABCD...Z#")

/**
响应点击索引时的委托方法,正常返回index即可,如果有特殊处理(比如点击一个索引滑动到其他section),可以在这个方法中进行。
*/
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  // tell table which section corresponds to section title/index (e.g. "B",1))

二、索引(UITableViewIndex)其他属性(非公开的)

系统只提供了上面几个方法,供我们使用,但是,在很多场景里面,这些方法对我们来说是不够用的。因此对我们来说就有两种方法处理特殊情况:1、重写索引视图,自定义UITableViewIndex;2、通过UITableViewIndex内部属性进行设置。
自定义UITableViewIndex,我们下节再说,我们先看第二种方法:通过UITableViewIndex内部属性进行设置。
1、UITableViewIndex的内部属性

titles      //不知名属性
font       //字体大小
drawingInsets  //拓展范围
indexColor  //字体颜色
indexBackgroundColor //背景颜色
indexTrackingBackgroundColor 
selectedSection  //选中之类
selectedSectionTitle  //猜测试 indexTitle
pastTop 
pastBottom

UITableViewIndex的内部属性是有限的,如果我们只是简单的设置一下字体、颜色等,可以通过UITableViewIndex的内部属性进行设置,简单方便。
ps:比如修改索引文本的字体大小

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    for (UIView *view in [tableView subviews]) {
        if ([view isKindOfClass:[NSClassFromString(@"UITableViewIndex") class]]) {
           // 设置字体大小
            [view setValue:[UIFont systemFontOfSize:12.0 weight:UIFontWeightRegular] forKey:@"_font"];
           //设置view的大小
            view.bounds = CGRectMake(0, 0, 30, 30);
            //单单设置其中一个是无效的
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,792评论 3 3
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,174评论 1 32
  • 一.UITextField属性 0.enablesReturnKeyAutomatically 默认为No,如果设...
    奋斗ing0310阅读 1,705评论 0 2
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,590评论 1 14
  • 老师对某些学生的言语…… 班里有俩人 上周末作业没交 老师:就光教这俩人费的心思 我这工资就不够……(呵呵) 对于...
    Sunnyfind阅读 209评论 1 1