一、表视图的索引目录
首先要创建一个TableView,之前有说过,这里就不详细说了(参考前面第十四篇)。
直接贴代码吧,
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{ UITableView *tableView; NSArray *list;//分组标题
NSDictionary *dic;//每行内容
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height; tableView = [[UITableView alloc] initWithFrame:(CGRect){,,width,height}];
tableView.dataSource = self;
tableView.delegate = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableView]; [self readySource];
} //在viewDidLoad方法中调用
- (void)readySource{ dic = @{@"A":@[@"adhere", @"adaft", @"abase", @"alarm", @"apace"],
@"B":@[@"babel", @"board", @"bili", @"band"],
@"C":@[@"cabbages", @"crray", @"china", @"chafe", @"cocos", @"core"],
@"D": @[@"dabbing", @"dacca", @"dady"],
@"E": @[@"email", @"each", @"eager", @"ebook", @"enable", @"embalm", @"eman"],
@"F": @[@"fear", @"faceBook", @"float", @"flour"],
@"G": @[@"getter", @"gaba", @"grace", @"great", @"gracious"],
@"H": @[@"header", @"haber", @"habit", @"hoard"],
};
list = dic.allKeys;
} //返回分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [list count];
} //返回每个分组中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//获取分组
NSString *key = [list objectAtIndex:section];
//获取分组里面的数组
NSArray *array = [dic objectForKey:key]; return [array count];
} - (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //索引路径
NSInteger section = [indexPath section];
NSInteger row = [indexPath row]; //获取分组
NSString *key = [list objectAtIndex:section]; //获取分组里面的数组
NSArray *array = [dic objectForKey:key]; //建立可重用标识符
static NSString *indentifier = @"UITableViewCell"; // NSString *indentifier = [NSString stringWithFormat:@"UITableViewCell%ld%ld",(long)indexPath.row,(long)indexPath.section]; UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:indentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
} //设置其辅助样式
cell.accessoryType = UITableViewCellAccessoryNone; //移除所有子视图
[cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIView *view = (UIView*)obj;
[view removeFromSuperview];
}]; //添加新视图
UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){,,,}];
NSString *str = [array objectAtIndex:row];
title.text = str;
title.font = [UIFont systemFontOfSize:];
title.textColor = [UIColor blueColor];
[cell addSubview:title]; return cell;
} //获取分组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSString *key = [list objectAtIndex:section];
return key;
} //给TableViewCell添加索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return list; } //点击目录
- (NSInteger)tableView:(UITableView *)TableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ //获取所点目录对应的IndexPath值
NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow: inSection:index]; //让Table滚动到对应的indexPath位置
[TableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
return index;
} //设置TableViewCell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return ;
}
效果图:
二、可以进行标记的表视图
首先要在- (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法中,把cell.accessoryType = UITableViewCellAccessoryNone;
//点击行事件
- (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //获取点击行的cell
UITableViewCell *cell = [TableView cellForRowAtIndexPath:indexPath]; //如果cell已经被标记
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
//取消标记
cell.accessoryType = UITableViewCellAccessoryNone;
}else
//反之,标记
cell.accessoryType = UITableViewCellAccessoryCheckmark; //取消选中效果
[TableView deselectRowAtIndexPath:indexPath animated:YES];
}
效果图: