iOS笔记UI--UITableview索引

列表索引的简单实现

//
//  ViewController.m
//  UITableView索引
//
//  Created by hhg on 15/10/8.
//  Copyright (c) 2015年 hhg. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) NSMutableArray *topTitleArray;
@property(nonatomic,strong) NSMutableArray *detailContentArray;
@property(nonatomic,strong) NSMutableArray *rightIndexArray;
@property(nonatomic,strong) NSMutableArray *flag;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    table.tag = 99;
    [self.view addSubview:table];

    table.dataSource = self;
    table.delegate = self;
}

#pragma mark  -  UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.topTitleArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *num = self.detailContentArray[section];
    if ([self.flag[section] isEqual:@NO]) {
        return 0;
    }
    return num.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * str = @"simpleCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }

    NSArray *num = self.detailContentArray[indexPath.section];
    cell.textLabel.text = num[indexPath.row];

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 49;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 33;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * view = [[UIView alloc]init];
    view.backgroundColor = [UIColor greenColor];

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    label.text = self.topTitleArray[section];
    [label setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:label];
    view.tag = section;      //用于搜索操作

    // 响应
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    [view addGestureRecognizer:tap];

    return view;
}

#pragma mark - 索引协议方法
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.rightIndexArray;
}

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

#pragma mark  - 段头点击
-(void)click:(UITapGestureRecognizer *)tap {
    UIView *view = tap.view;
    if ([self.flag[view.tag] isEqual:@NO]) {
        self.flag[view.tag] = @YES;
    } else {
        self.flag[view.tag] = @NO;
    }
    UITableView *table = (UITableView *)[self.view viewWithTag:99];
    [table reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Data
- (NSMutableArray *)topTitleArray {
    if (!_topTitleArray) {
        _topTitleArray = [self creaMutableArray];
    }
    return _topTitleArray;
}

- (NSMutableArray *)detailContentArray {
    if (!_detailContentArray) {
        _detailContentArray = [NSMutableArray array];
        for (char i = 'A'; i <= 'Z'; i++) {
            [_detailContentArray addObject:@[[NSString stringWithFormat:@"%c1",i],
                                             [NSString stringWithFormat:@"%c2",i],
                                             [NSString stringWithFormat:@"%c3",i],
                                             [NSString stringWithFormat:@"%c4",i],
                                             [NSString stringWithFormat:@"%c5",i],
                                             ]];
        }
        [_detailContentArray addObject:@"#"];
    }
    return _detailContentArray;
}

- (NSMutableArray *)rightIndexArray {
    if (!_rightIndexArray) {
        _rightIndexArray = [self creaMutableArray];
    }
    return _rightIndexArray;
}

- (NSMutableArray *)flag {
    if (!_flag) {
        _flag = [NSMutableArray array];
        for (char i = 'A'; i <= 'Z'; i++) {
            [_flag addObject:@YES];
        }
    }
    return _flag;
}

#pragma mark - factory
- (NSMutableArray *)creaMutableArray {
    NSMutableArray* mutableArray = [NSMutableArray array];
    for (char i = 'A'; i <= 'Z'; i++) {
        [mutableArray addObject:[NSString stringWithFormat:@"%c",i]];
    }
    return mutableArray;
}

@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值