一、表视图的索引目录

首先要创建一个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];
}

效果图:

ios基础篇(二十六)—— UITableViewCell的分组索引与标记的更多相关文章

  1. ios基础篇(十六)——UIWebView的基本使用

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...

  2. ios基础篇(十四)——UITableView(二)属性及基本用法

    上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法. 一.属性 1.frame:设置控件的尺寸和大小 2.backg ...

  3. ios基础篇(十二)——UINavgationController的使用(三)ToolBar

    UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏:设置UINavigationController的toolbarHidden属性可显示UIToolB ...

  4. ioS基础篇(十九)——UIResponder简析

    UIResponder类定义了对象相应和控制事件的接口,他是UIApplication.UIView的超类,这类的实例通常被称为应答对象. 一.Responder对象 在iOS系统中,能够响应并处理事 ...

  5. ios基础篇(十八)——Delegate 、NSNotification 和 KVO用法及其区别

    一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能. ...

  6. iOS基础篇(十五)——UIScrollView的基本用法

    滚动视图(UIScrollView)通常用于显示内容尺寸大于屏幕尺寸的视图. 一.基本属性 1.CGSize contentSize :设置UIScrollView的滚动范围 2.CGPoint co ...

  7. Java基础(二十六)Java IO(3)字节流(Byte Stream)

    字节流是以字节为单位来处理数据的,由于字节流不会对数据进行任何转换,因此用来处理二进制的数据. 一.InputStream类与OutputStream类 1.InputStream类是所有字节输入流的 ...

  8. JavaEE基础(二十六)/网络

    1.网络编程(网络编程概述) A:计算机网络 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信 ...

  9. ios基础篇(十)——UINavgationController的使用(一)UIBarButtonItem的添加

    UINavigationController又被成为导航控制器,继承自UIViewController,以栈的方式管理所控制的视图控制器,下面就详细说一下UINavigationController的 ...

随机推荐

  1. jQuery Mobile应用之火车票查询

    效果图: 在CMD中输入如下代码 corsproxy (前提是有node.js环境,并先安装corsproxy) html: <!DOCTYPE html> <html> &l ...

  2. ajax动态添加的li不能绑定click事件

    单纯的给li标签添加click事件,是不会执行的. 经过试验 <ul id="searchedUser"><li>搜索结果</li></u ...

  3. php常用数组函数回顾一

    数组对于程序开发来说是一个必不可少的工具,我根据网上的常用数组函数,结合个人的使用情况,进行数组系列的总结复习.里面当然不只是数组的基本用法,还有相似函数的不同用法的简单实例,力求用最简单的实例,记住 ...

  4. [ASM C/C++] C语言数组

    固定长度的数组: 可以具有任何的存储类别. 长度可变的数组: 只能具有自动的生存周期(定义于语句块内,眀没有static修饰符). 名字必须为一般的标识符,因此结构或联合的成员不能是数组的标识符. 读 ...

  5. iOS runtime实用篇解决常见Crash

    程序崩溃经历 其实在很早之前就想写这篇文章了,一直拖到现在. 程序崩溃经历1 平时开发测试的时候好好的,结果上线几天发现有崩溃的问题,其实责任大部分在我身上. 我的责任: 过分信赖文档,没进行容错处理 ...

  6. 初识NodeJS,一个基于GoogleV8引擎的Javascript运行环境

    思考 首先我们来思考一个问题:我们都知道几乎所有现代主流浏览器都全面支持了ECMAScript 5.1版标准,而JavaScript的标准是ECMAScript.那么我们就容易认为JavaScript ...

  7. ionic 中$ionicView.beforeEnter 事件的一个bug

    我在使用ionic写app的时候,需要使用$IonicView.beforeEnter事件,在页面进入前做一些事情,但是发现,它不起作用,很蛋疼,后来,看了别人做的app例子,也涉及到这个$Ionic ...

  8. EXCEL表格实现万位分隔符效果!

    单击单元格右键 选择自定义单元格格式 选择数字标签 选择自定义 在输入框中输入:###","#### 单击确定即可! 格式刷可以对其他单元格实行同样效果!

  9. django queryset values&values_list

    values返回是字典列表; values_list返回的是元组列表, values_list加上 flat=True 1 1 之后返回值列表

  10. sql 游标

    --创建游标 DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ] [ FORWARD_ONLY | SCROLL ] [ STATIC | KEYSET | D ...