索引初认 主要实现的方法
- 索引UI
// 每个分区的页眉
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionTitles objectAtIndex:section];
}
// 索引目录
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return sectionTitles;
}
- 点击索引,跳转到点击的分区
// 点击目录
-(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;
}
资料