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. SEO之关键词选择

    在网站优化中,关键词应该是奠基石,选择好关键词的重要性也不言而喻了.怎样选择合理化的关键词呢?这个是我今天了解到的. 1.选择的关键词首先是有人搜索过的.没人搜索的词优化就是浪费时间. 2.做有效流量 ...

  2. string 数字序列大小比较

    string 数字序列大小比较 string.compare string a = "022"; string b="1"; 比较结果 '022' < ' ...

  3. Chapter 19_0 位操作库

    位操作库是Lua5.2版本里添加的库,所有函数放在bit32 table里.(bit32只能针对32位整数运算) 在Lua5.3版本里,bit32库被废弃掉.不过可以使用一个外部兼容库,但是最好直接用 ...

  4. PHP程序员的技术成长规划(转)

    按照了解的很多PHP/LNMP程序员的发展轨迹,结合个人经验体会,抽象出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定 ...

  5. Java处理JPEG图片时,需要导入com.sun.image.codec.jpeg.JPEGImageEn,报错处理

    Java处理JPEG图片时,需要导入com.sun.image.codec.jpeg.JPEGImageEn,会报错,不能使用相应的方法. 原因:java访问限制级api的时候,默认的eclipse设 ...

  6. C# DataTable转实体通用方法

    public static class DataTableHelper { public static T GetEntity<T>(DataTable table) where T : ...

  7. Maven项目下WEB-INFO目录下没有编译的classes文件

    建立mavan项目之后,在项目目录中没有发现编译的classes文件夹 解决办法: 因为maven是默认将编译后的classes文件存入项目下的target文件夹中,所以我们需要修改编译后存放的路径, ...

  8. Eclipse 打开当前文件所在的文件夹

    两种方法: 1.安装EasyExplorer插件,有了这个插件就可以很方便地打开资源文件所在的文件夹了. EasyExplorer 从 http://sourceforge.net/projects/ ...

  9. Digi. Certificates: Key pairs usages

    In short, we have some sort of algorithms to gen pair of private and public keys. The public key is ...

  10. Dubbo.xml配置源-Dubbo.xsd分析

      我们使用Dubbo时,一般都会使用xml配置基本信息,如项目名称(application).注册中心(register).协议(protocal).服务(service),如下所示: 1 2 3 ...