UITableView 索引詳細(xì)

系統(tǒng)默認(rèn)

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

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

設(shè)置導(dǎo)航字體顏色、字體、背景色(常用)

     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]];
                   }

        }

實(shí)際效果

自定義索引

方法封裝 (參考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

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

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

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