iOS中UITableView的索引设置

看到很多app的关于UITableView的页面在右手边,都有一系列的索引设置;简单的学习了下,其实主要是调用了UITableView的相关代理方法来实现的:

主要是实现下面四个方法:

//返回section中的row

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

//返回每个索引的内容

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//返回索引数组

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

//响应点击索引时的委托方法

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;


当然在UITableView中还有关于索引的相关属性设置如下:

 _myTableView.sectionIndexColor = [UIColorblueColor];//设置默认时索引值颜色

 _myTableView.sectionIndexTrackingBackgroundColor = [UIColorgrayColor];//设置选中时,索引背景颜色

 _myTableView.sectionIndexBackgroundColor = [UIColorclearColor];// 设置默认时,索引的背景颜色


相关代码如下所示:

@interface ViewController (){
    
    NSMutableArray *indexs;         //索引数组
    
    NSMutableArray *_titleArray;    //表中内容
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    _titleArray = [NSMutableArray array];
    
    indexs = [NSMutableArray array];

    for(char c = 'A'; c <= 'Z'; c++ )
    {
        [indexs addObject:[NSString stringWithFormat:@"%c",c]];
        [_titleArray addObject:[NSString stringWithFormat:@"%c",c]];
        [_titleArray addObject:[NSString stringWithFormat:@"%c",c]];
    }
    
    //初始化数据
    [self initMyTableView];
}

//初始化UITableView
- (void)initMyTableView{
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
    _myTableView.showsVerticalScrollIndicator = NO;
    _myTableView.sectionIndexColor = [UIColor blueColor];
    _myTableView.sectionIndexTrackingBackgroundColor = [UIColor grayColor];
    _myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
    [_myTableView setDataSource:self];
    [_myTableView setDelegate:self];
    
    [self.view addSubview:_myTableView];
}

#pragma mark UITableViewDataSource
//返回section中的row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *Identify = @"CELL";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identify];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identify];
    }
    
    cell.textLabel.text =  [_titleArray objectAtIndex:indexPath.section * 2 + indexPath.row];
    
    return cell;
}

//返回索引数组
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return indexs;
}

//响应点击索引时的委托方法
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    
    NSInteger count = 0;
    
    for (NSString *character in indexs) {

        if ([[character uppercaseString] hasPrefix:title]) {
            return count;
        }
        
        count++;
    }
    
    return  0;
}

//返回每个索引的内容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return [indexs objectAtIndex:section];
}

//返回section的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return [indexs count];
}




效果图如下:



  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值