列表索引的简单实现
//
// 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
本文介绍了一个简单的UITableView索引实现方法,通过创建自定义的ViewController来展示如何使用UITableView的数据源和代理方法实现列表索引功能。文章详细展示了如何设置UITableView的section、cell以及头部视图,并实现了点击头部视图展开或收起对应section的功能。
692

被折叠的 条评论
为什么被折叠?



