• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Bo.Ke
博客园    首页    新随笔    联系   管理    订阅  订阅
ios基础篇(二十六)—— UITableViewCell的分组索引与标记

一、表视图的索引目录

首先要创建一个TableView,之前有说过,这里就不详细说了(参考前面第十四篇)。

直接贴代码吧,

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
  4 
  5     UITableView *tableView;
  6     
  7     NSArray *list;//分组标题
  8     NSDictionary *dic;//每行内容
  9 }
 10 
 11 @end
 12 
 13 @implementation ViewController
 14 
 15 - (void)viewDidLoad {
 16     [super viewDidLoad];
 17     // Do any additional setup after loading the view, typically from a nib.
 18     CGFloat width = self.view.frame.size.width;
 19     CGFloat height = self.view.frame.size.height;
 20     
 21     tableView = [[UITableView alloc] initWithFrame:(CGRect){0,20,width,height}];
 22     tableView.dataSource = self;
 23     tableView.delegate = self;
 24     tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 25     [self.view addSubview:tableView];
 26     
 27     [self readySource];
 28 }
 29 
 30 //在viewDidLoad方法中调用
 31 - (void)readySource{
 32     
 33     dic = @{@"A":@[@"adhere", @"adaft", @"abase", @"alarm", @"apace"],
 34             @"B":@[@"babel", @"board", @"bili", @"band"],
 35             @"C":@[@"cabbages", @"crray", @"china", @"chafe", @"cocos", @"core"],
 36             @"D": @[@"dabbing", @"dacca", @"dady"],
 37             @"E": @[@"email", @"each", @"eager", @"ebook", @"enable", @"embalm", @"eman"],
 38             @"F": @[@"fear", @"faceBook", @"float", @"flour"],
 39             @"G": @[@"getter", @"gaba", @"grace", @"great", @"gracious"],
 40             @"H": @[@"header", @"haber", @"habit", @"hoard"],
 41             };
 42     list = dic.allKeys;
 43 }
 44 
 45 //返回分组个数
 46 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 47     return [list count];
 48 }
 49 
 50 //返回每个分组中的行数
 51 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 52     //获取分组
 53     NSString *key = [list objectAtIndex:section];
 54     //获取分组里面的数组
 55     NSArray *array = [dic objectForKey:key];
 56     
 57     return [array count];
 58 }
 59 
 60 - (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 61 
 62     //索引路径
 63     NSInteger section = [indexPath section];
 64     NSInteger row = [indexPath row];
 65     
 66     //获取分组
 67     NSString *key = [list objectAtIndex:section];
 68     
 69     //获取分组里面的数组
 70     NSArray *array = [dic objectForKey:key];
 71     
 72     //建立可重用标识符
 73     static NSString *indentifier = @"UITableViewCell";
 74     
 75 //    NSString *indentifier = [NSString stringWithFormat:@"UITableViewCell%ld%ld",(long)indexPath.row,(long)indexPath.section];
 76     
 77     UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:indentifier];
 78     if (!cell) {
 79         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
 80     }
 81     
 82     //设置其辅助样式
 83     cell.accessoryType = UITableViewCellAccessoryNone;
 84     
 85     //移除所有子视图
 86     [cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
 87         UIView *view = (UIView*)obj;
 88         [view removeFromSuperview];
 89     }];
 90     
 91     //添加新视图
 92     UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){20,10,200,30}];
 93     NSString *str = [array objectAtIndex:row];
 94     title.text = str;
 95     title.font = [UIFont systemFontOfSize:20];
 96     title.textColor = [UIColor blueColor];
 97     [cell addSubview:title];
 98     
 99     return cell;
100 }
101 
102 //获取分组标题
103 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
104 
105     NSString *key = [list objectAtIndex:section];
106     return key;
107 }
108 
109 //给TableViewCell添加索引
110 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
111     
112     return list;
113     
114 }
115 
116 //点击目录
117 - (NSInteger)tableView:(UITableView *)TableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
118 
119 //获取所点目录对应的IndexPath值
120     NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
121     
122 //让Table滚动到对应的indexPath位置
123     [TableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
124     return index;
125 }
126 
127 //设置TableViewCell行高
128 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
129 
130     return 50;
131 }

效果图:

二、可以进行标记的表视图

首先要在- (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法中,把cell.accessoryType = UITableViewCellAccessoryNone;

 1 //点击行事件
 2 - (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 3 
 4     //获取点击行的cell
 5     UITableViewCell *cell = [TableView cellForRowAtIndexPath:indexPath];
 6     
 7     //如果cell已经被标记
 8     if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
 9         //取消标记
10         cell.accessoryType = UITableViewCellAccessoryNone;
11     }else
12         //反之,标记
13         cell.accessoryType = UITableViewCellAccessoryCheckmark;
14     
15     //取消选中效果
16     [TableView deselectRowAtIndexPath:indexPath animated:YES];
17 }

效果图:

posted on 2016-01-04 13:55  Bo.Ke  阅读(534)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3