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. hdu 4122 Alice's mooncake shop(单调队列)

    题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...

  2. [SOJ] Ordering Tasks

    1940. Ordering Tasks Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description John has n task ...

  3. 【IE6的疯狂之三】IE6 3像素BUG的实例

    问题:2列布局.左列固定,右列液态我需要做一个布局.2列,左边列固定宽度.右边列使用剩余宽度.整体宽度不固定,这样不管在17 还是19的屏幕上,左边列始终宽度不变,右边列宽度始终占据剩余宽度.但是我写 ...

  4. 关于pagerank算法的一点点总结

    1. PageRank算法每个顶点收敛的值与每个点的初值是没有关系的,每个点随便赋初值. 2.像q=0.8这样的阻尼系数已经解决了PageRank中处在的孤立点问题.黑洞效应问题. 3.当有那个点进行 ...

  5. EFI、GPT和BIOS、MBR

    用了数十年的PC机主板架构是BIOS模式.但在2004年,微软和英特尔共同推出一种名为可扩展固件接口(EFI)的主板升级换代方案.EFI,即可扩展固件接口(Extensible Firmware In ...

  6. linux下获取硬盘、内存、U盘大小及使用大小

    /* * 获取硬盘大小;内存大小;usb大小 */ #ifndef SYSINFOGET_H #define SYSINFOGET_H #include <stdio.h> //磁盘信息 ...

  7. X264的版本号

    0 X264官方地扯 http://www.videolan.org/developers/x264.html 1 X264官方编译的二进制程序命名格式 官方编译出了LINUX,Win32,Win64 ...

  8. Mac下MySQL的安装与配置

    之前一直用的是阿里云的服务器,在服务器上装了一个MySQL,但是今天发现到期了,而且续费时发现之前的大学生优惠不能用了,可是明明到6月份,大学生才毕业啊,shit!!!所以没办法只能在自己电脑上装一个 ...

  9. @property (nonatomic, getter = isExpanded) BOOL expanded;

    如果这个property是 BOOL on, 那么Objc默认创建的 setter 为: - (void)on:(BOOL)setOn { } getter 为: - (BOOL)on { retur ...

  10. JAVA语法基础(课堂ppt问题总结)

    一:运行源代码EnumTest.java,分析运行结果. 代码如下: public class EnumTest { public static void main(String[] args) { ...