typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {

UITableViewCellAccessoryNone, // 不显示任何图标

UITableViewCellAccessoryDisclosureIndicator, // 跳转指示图标

UITableViewCellAccessoryDetailDisclosureButton, // 内容详情图标和跳转指示图标

UITableViewCellAccessoryCheckmark, // 勾选图标

UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // 内容详情图标

};

通过前面的演示这里简单总结一些UITableView的刷新方法:

- (void)reloadData;刷新整个表格。

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);刷新指定的

分组和行。

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);刷新指定的分组。

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;删除时刷新指定的行数据。

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;添加时刷新指定的行数据。

#pragma mark - 数据源方法

#pragma mark 返回分组数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

NSLog(@"计算分组数");

return _contacts.count;

}

#pragma mark 返回每组行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSLog(@"计算每组(组%i)行数",section);

KCContactGroup *group1=_contacts[section];

return group1.contacts.count;

}

#pragma mark返回每行的单元格

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//NSIndexPath是一个对象,记录了组和行信息

NSLog(@"生成单元格(组:%i,行%i)",indexPath.section,indexPath.row);

KCContactGroup *group=_contacts[indexPath.section];

KCContact *contact=group.contacts[indexPath.row];

UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];

cell.textLabel.text=[contact getName];

cell.detailTextLabel.text=contact.phoneNumber;

return cell;

}

#pragma mark 返回每组头标题名称

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

NSLog(@"生成组(组%i)名称",section);

KCContactGroup *group=_contacts[section];

return group.name;

}

#pragma mark 返回每组尾部说明

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

NSLog(@"生成尾部(组%i)详情",section);

KCContactGroup *group=_contacts[section];

return group.detail;

}

#pragma mark 返回每组标题索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

NSLog(@"生成组索引");

NSMutableArray *indexs=[[NSMutableArray alloc]init];

for(KCContactGroup *group in _contacts){

[indexs addObject:group.name];

}

return indexs;

}

#pragma mark - 代理方法

#pragma mark 设置分组标题内容高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

if(section==0){

return 50;

}

return 40;

}

#pragma mark 设置每行高度(每行高度可以不一样)

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 45;

}

#pragma mark 设置尾部说明内容高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

return 40;

}

pragma mark 点击行

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

_selectedIndexPath=indexPath;

KCContactGroup *group=_contacts[indexPath.section];

KCContact *contact=group.contacts[indexPath.row];

//创建弹出窗口

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"System Info" message:[contact getName] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

alert.alertViewStyle=UIAlertViewStylePlainTextInput; //设置窗口内容样式

UITextField *textField= [alert textFieldAtIndex:0]; //取得文本框

textField.text=contact.phoneNumber; //设置文本框内容

[alert show]; //显示窗口

}

#pragma mark 窗口的代理方法,用户保存数据

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

//当点击了第二个按钮(OK)

if (buttonIndex==1) {

UITextField *textField= [alertView textFieldAtIndex:0];

//修改模型数据

KCContactGroup *group=_contacts[_selectedIndexPath.section];

KCContact *contact=group.contacts[_selectedIndexPath.row];

contact.phoneNumber=textField.text;

//刷新表格

[_tableView reloadData];

}

}

#pragma mark 重写状态样式方法

-(UIStatusBarStyle)preferredStatusBarStyle{

return UIStatusBarStyleLightContent;

}

#

pragma mark 窗口的代理方法,用户保存数据

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

//当点击了第二个按钮(OK)

if (buttonIndex==1) {

UITextField *textField= [alertView textFieldAtIndex:0];

//修改模型数据

KCContactGroup *group=_contacts[_selectedIndexPath.section];

KCContact *contact=group.contacts[_selectedIndexPath.row];

contact.phoneNumber=textField.text;

//刷新表格

NSArray *indexPaths=@[_selectedIndexPath];//需要局部刷新的单元格的组、行

[_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];//后面的参数代表更新时的动画

}

}

#pragma mark返回每行的单元格

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//NSIndexPath是一个对象,记录了组和行信息

NSLog(@"生成单元格(组:%i,行%i)",indexPath.section,indexPath.row);

KCContactGroup *group=_contacts[indexPath.section];

KCContact *contact=group.contacts[indexPath.row];

//由于此方法调用十分频繁,cell的标示声明成静态变量有利于性能优化

static NSString *cellIdentifier=@"UITableViewCellIdentifierKey1";

//首先根据标识去缓存池取

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

//如果缓存池没有到则重新创建并放到缓存池中

if(!cell){

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];

}

cell.textLabel.text=[contact getName];

cell.detailTextLabel.text=contact.phoneNumber;

NSLog(@"cell:%@",cell);

return cell;

}

#pragma mark 点击行

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

_selectedIndexPath=indexPath;

KCContactGroup *group=_contacts[indexPath.section];

KCContact *contact=group.contacts[indexPath.row];

//创建弹出窗口

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"System Info" message:[contact getName] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

alert.alertViewStyle=UIAlertViewStylePlainTextInput; //设置窗口内容样式

UITextField *textField= [alert textFieldAtIndex:0]; //取得文本框

textField.text=contact.phoneNumber; //设置文本框内容

[alert show]; //显示窗口

}

#pragma mark 删除操作

//实现了此方法向左滑动就会显示删除按钮

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

KCContactGroup *group =_contacts[indexPath.section];

KCContact *contact=group.contacts[indexPath.row];

if (editingStyle==UITableViewCellEditingStyleDelete) {

[group.contacts removeObject:contact];

//考虑到性能这里不建议使用reloadData

//[tableView reloadData];

//使用下面的方法既可以局部刷新又有动画效果

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

//如果当前组中没有数据则移除组刷新整个表格

if (group.contacts.count==0) {

[_contacts removeObject:group];

[tableView reloadData];

}

}

}

#pragma mark 取得当前操作状态,根据不同的状态左侧出现不同的操作按钮

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

if (_isInsert) {

return UITableViewCellEditingStyleInsert;

}

return UITableViewCellEditingStyleDelete;

}

#pragma mark 编辑操作(删除或添加)

//实现了此方法向左滑动就会显示删除(或添加)图标

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

KCContactGroup *group =_contacts[indexPath.section];

KCContact *contact=group.contacts[indexPath.row];

if (editingStyle==UITableViewCellEditingStyleDelete) {

[group.contacts removeObject:contact];

//考虑到性能这里不建议使用reloadData

//[tableView reloadData];

//使用下面的方法既可以局部刷新又有动画效果

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

//如果当前组中没有数据则移除组刷新整个表格

if (group.contacts.count==0) {

[_contacts removeObject:group];

tableView reloadData];

}

}else if(editingStyle==UITableViewCellEditingStyleInsert){

KCContact *newContact=[[KCContact alloc]init];

newContact.firstName=@"first";

newContact.lastName=@"last";

newContact.phoneNumber=@"12345678901";

[group.contacts insertObject:newContact atIndex:indexPath.row];

[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//注意这里没有使用reladData刷新

}

}

#pragma mark 排序

//只要实现这个方法在编辑状态右侧就有排序图标

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

KCContactGroup *sourceGroup =_contacts[sourceIndexPath.section];

KCContact *sourceContact=sourceGroup.contacts[sourceIndexPath.row];

KCContactGroup *destinationGroup =_contacts[destinationIndexPath.section];

[sourceGroup.contacts removeObject:sourceContact];

if(sourceGroup.contacts.count==0){

[_contacts removeObject:sourceGroup];

[tableView reloadData];

}

[destinationGroup.contacts insertObject:sourceContact atIndex:destinationIndexPath.row];//主要的一句代码

}

#pragma mark - 搜索框代理

#pragma mark 取消搜索

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{

_isSearching=NO;

_searchBar.text=@"";

[self.tableView reloadData];

}

#pragma mark 输入搜索关键字

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

if([_searchBar.text isEqual:@""]){

_isSearching=NO;

[self.tableView reloadData];

return;

}

[self searchDataWithKeyWord:_searchBar.text];

}

#pragma mark 点击虚拟键盘上的搜索时

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

[self searchDataWithKeyWord:_searchBar.text];

[_searchBar resignFirstResponder];//放弃第一响应者对象,关闭虚拟键盘

}

#pragma mark 搜索形成新数据

-(void)searchDataWithKeyWord:(NSString *)keyWord{

_isSearching=YES;

_searchContacts=[NSMutableArray array];

[_contacts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

KCContactGroup *group=obj;

[group.contacts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

KCContact *contact=obj;

if ([contact.firstName.uppercaseString containsString:keyWord.uppercaseString]||[contact.lastName.uppercaseString containsString:keyWord.uppercaseString]||[contact.phoneNumber

containsString:keyWord]) {

[_searchContacts addObject:contact];

}

}];

}];

//刷新表格

[self.tableView reloadData];

}

pragma mark 添加搜索栏

-(void)addSearchBar{

CGRect searchBarRect=CGRectMake(0, 0, self.view.frame.size.width, kSearchbarHeight);

_searchBar=[[UISearchBar alloc]initWithFrame:searchBarRect];

_searchBar.placeholder=@"Please input key word...";

//_searchBar.keyboardType=UIKeyboardTypeAlphabet;//键盘类型

//_searchBar.autocorrectionType=UITextAutocorrectionTypeNo;//自动纠错类型

//_searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;//哪一次shitf被自动按下

_searchBar.showsCancelButton=YES;//显示取消按钮

//添加搜索框到页眉位置

_searchBar.delegate=self;

self.tableView.tableHeaderView=_searchBar;

}

点击cell后恢复原来的颜色 [tableView deselectRowAtIndexPath:indexPath animated:YES];

tableview 代理方法详解的更多相关文章

  1. IOS UITableView的代理方法详解

    一.UITableViewDataSourc(数据源代理) 1.必须实现的回调方法 返回每个分区的行数 - (NSInteger)tableView:(UITableView *)tableView ...

  2. HTTP请求方法详解

    HTTP请求方法详解 请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源]     GET方法用来请求已被URI识别的资源.指定 ...

  3. CURL使用方法详解

    php采集神器CURL使用方法详解 作者:佚名  更新时间:2016-10-21   对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程 ...

  4. PHP cURL应用实现模拟登录与采集使用方法详解

    对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情景,file_get_co ...

  5. Python基础之 urllib模块urlopen()与urlretrieve()的使用方法详解。

    Python urllib模块urlopen()与urlretrieve()的使用方法详解   1.urlopen()方法urllib.urlopen(url[, data[, proxies]]) ...

  6. Java 反射 设计模式 动态代理机制详解 [ 转载 ]

    Java 反射 设计模式 动态代理机制详解 [ 转载 ] @author 亦山 原文链接:http://blog.csdn.net/luanlouis/article/details/24589193 ...

  7. PHP cURL实现模拟登录与采集使用方法详解教程

    来源:http://www.zjmainstay.cn/php-curl 本文将通过案例,整合浏览器工具与PHP程序,教你如何让数据 唾手可得 . 对于做过数据采集的人来说,cURL一定不会陌生.虽然 ...

  8. 利用C#实现AOP常见的几种方法详解

    利用C#实现AOP常见的几种方法详解 AOP面向切面编程(Aspect Oriented Programming) 是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 下面这篇文章主要 ...

  9. 3、lvs调度方法详解

    3.lvs类型和调度方法详解    http://www.178linux.com/13570 集群:将多台主机组织起来满足某一特定需求: 集群类型: LB:Load Balancing, 负载均衡集 ...

随机推荐

  1. object model 概述

    Object Model 综述 标准 C++ 的对象模型为对象的动态特性提供了运行时的支持. 但是它静态的本性决定了在某些领域它表现出僵化.不可扩展的特点. GUI编程就是一个既需要运行时编译的效率, ...

  2. Python学习笔记——基础篇【第五周】——算法(4*4的2维数组和冒泡排序)、时间复杂度

    目录 1.算法基础 2.冒泡排序 3.时间复杂度 (1)时间频度 (2)时间复杂度 4.指数时间 5.常数时间 6.对数时间 7.线性时间 1.算法基础  要求:生成一个4*4的2维数组并将其顺时针旋 ...

  3. [Q]pdfFactory虚拟打印机的安装

    安装打图精灵过程中会提示是否安装pdfFactory虚拟打印机,建议选择安装. 若未安装,在安装打图精灵之后想安装pdfFactory,该软件可以在打图精灵应用程序文件夹下找到( 系统"开始 ...

  4. [转] Gvim for windows中块选择的方法

    在gvim(windows版)中,块选择的快捷键不是<Ctrl-v>,此快捷键为粘贴. 一般的选择模式为:v(小写),此时会显示:可视. 行选择模式为:V(大写),此时会显示:可视-行. ...

  5. Python调用(运行)外部程序

    在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...

  6. 用微软makecert.exe生成一个自签名的证书

    RT makecert.exe不用去找,安装VS2008后,在开始菜单相应的路径找到该命令提示符:Microsoft Visual Studio 2008/Visual Studio Tools/Vi ...

  7. MAMP、wordpress安装

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Helvetica } p.p4 { margin: ...

  8. /home 和 /root

    /root     Linux超级权限用户root的家目录./home 如果我们建立一个用户,用户名是"xx",那么在/home目录下就有一个对应的/home/xx路径,用来存放用 ...

  9. tableView区头不显示

    不知道什么原因 如果设置tableView的样式为Group 则必须写代理 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; co ...

  10. [Mark] KVM 虚拟化基本原理

    X86 操作系统是设计在直接运行在裸硬件设备上的,因此它们自动认为它们完全占有计算机硬件.x86 架构提供四个特权级别给操作系统和应用程序来访问硬件. Ring 是指 CPU 的运行级别,Ring 0 ...