UITableView全面解析
本文转自:http://www.cocoachina.com/ios/20140922/9710.html
在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信、QQ、新浪微博等软件基本上随处都是UITableView。当然它的广泛使用自然离不开它强大的功能,今天这篇文章将针对UITableView重点展开讨论。今天的主要内容包括:



- typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
- UITableViewCellStyleDefault, // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
- UITableViewCellStyleValue1, // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
- UITableViewCellStyleValue2, // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
- UITableViewCellStyleSubtitle // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
- };
- //
- // Contact.h
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import
- @interface KCContact : NSObject
- #pragma mark 姓
- @property (nonatomic,copy) NSString *firstName;
- #pragma mark 名
- @property (nonatomic,copy) NSString *lastName;
- #pragma mark 手机号码
- @property (nonatomic,copy) NSString *phoneNumber;
- #pragma mark 带参数的构造函数
- -(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber;
- #pragma mark 取得姓名
- -(NSString *)getName;
- #pragma mark 带参数的静态对象初始化方法
- +(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber;
- @end
- //
- // Contact.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCContact.h"
- @implementation KCContact
- -(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber{
- if(self=[super init]){
- self.firstName=firstName;
- self.lastName=lastName;
- self.phoneNumber=phoneNumber;
- }
- return self;
- }
- -(NSString *)getName{
- return [NSString stringWithFormat:@"%@ %@",_lastName,_firstName];
- }
- +(KCContact *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber{
- KCContact *contact1=[[KCContact alloc]initWithFirstName:firstName andLastName:lastName andPhoneNumber:phoneNumber];
- return contact1;
- }
- @end
- //
- // KCContactGroup.h
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import
- #import "KCContact.h"
- @interface KCContactGroup : NSObject
- #pragma mark 组名
- @property (nonatomic,copy) NSString *name;
- #pragma mark 分组描述
- @property (nonatomic,copy) NSString *detail;
- #pragma mark 联系人
- @property (nonatomic,strong) NSMutableArray *contacts;
- #pragma mark 带参数个构造函数
- -(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts;
- #pragma mark 静态初始化方法
- +(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts;
- @end
- //
- // KCContactGroup.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCContactGroup.h"
- @implementation KCContactGroup
- -(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts{
- if (self=[super init]) {
- self.name=name;
- self.detail=detail;
- self.contacts=contacts;
- }
- return self;
- }
- +(KCContactGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts{
- KCContactGroup *group1=[[KCContactGroup alloc]initWithName:name andDetail:detail andContacts:contacts];
- return group1;
- }
- @end
- //
- // KCMainViewController.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCMainViewController.h"
- #import "KCContact.h"
- #import "KCContactGroup.h"
- @interface KCMainViewController (){
- UITableView *_tableView;
- NSMutableArray *_contacts;//联系人模型
- }
- @end
- @implementation KCMainViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //初始化数据
- [self initData];
- //创建一个分组样式的UITableView
- _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
- //设置数据源,注意必须实现对应的UITableViewDataSource协议
- _tableView.dataSource=self;
- [self.view addSubview:_tableView];
- }
- #pragma mark 加载数据
- -(void)initData{
- _contacts=[[NSMutableArray alloc]init];
- KCContact *contact1=[KCContact initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"];
- KCContact *contact2=[KCContact initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"];
- KCContactGroup *group1=[KCContactGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]];
- [_contacts addObject:group1];
- KCContact *contact3=[KCContact initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"];
- KCContact *contact4=[KCContact initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"];
- KCContact *contact5=[KCContact initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"];
- KCContactGroup *group2=[KCContactGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]];
- [_contacts addObject:group2];
- KCContact *contact6=[KCContact initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"];
- KCContact *contact7=[KCContact initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"];
- KCContactGroup *group3=[KCContactGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]];
- [_contacts addObject:group3];
- KCContact *contact8=[KCContact initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"];
- KCContact *contact9=[KCContact initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"];
- KCContact *contact10=[KCContact initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"];
- KCContact *contact11=[KCContact initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"];
- KCContact *contact12=[KCContact initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"];
- KCContactGroup *group4=[KCContactGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]];
- [_contacts addObject:group4];
- KCContact *contact13=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"];
- KCContact *contact14=[KCContact initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"];
- KCContact *contact15=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"];
- KCContactGroup *group5=[KCContactGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]];
- [_contacts addObject:group5];
- }
- #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:UITableViewCellStyleDefault 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;
- }
- @end

- #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;
- }
- //
- // KCMainViewController.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCMainViewController.h"
- #import "KCContact.h"
- #import "KCContactGroup.h"
- @interface KCMainViewController ()<uitableviewdatasource,uitableviewdelegate,uialertviewdelegate>{
- UITableView *_tableView;
- NSMutableArray *_contacts;//联系人模型
- NSIndexPath *_selectedIndexPath;//当前选中的组和行
- }
- @end
- @implementation KCMainViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //初始化数据
- [self initData];
- //创建一个分组样式的UITableView
- _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
- //设置数据源,注意必须实现对应的UITableViewDataSource协议
- _tableView.dataSource=self;
- //设置代理
- _tableView.delegate=self;
- [self.view addSubview:_tableView];
- }
- #pragma mark 加载数据
- -(void)initData{
- _contacts=[[NSMutableArray alloc]init];
- KCContact *contact1=[KCContact initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"];
- KCContact *contact2=[KCContact initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"];
- KCContactGroup *group1=[KCContactGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]];
- [_contacts addObject:group1];
- KCContact *contact3=[KCContact initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"];
- KCContact *contact4=[KCContact initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"];
- KCContact *contact5=[KCContact initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"];
- KCContactGroup *group2=[KCContactGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]];
- [_contacts addObject:group2];
- KCContact *contact6=[KCContact initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"];
- KCContact *contact7=[KCContact initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"];
- KCContactGroup *group3=[KCContactGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]];
- [_contacts addObject:group3];
- KCContact *contact8=[KCContact initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"];
- KCContact *contact9=[KCContact initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"];
- KCContact *contact10=[KCContact initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"];
- KCContact *contact11=[KCContact initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"];
- KCContact *contact12=[KCContact initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"];
- KCContactGroup *group4=[KCContactGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]];
- [_contacts addObject:group4];
- KCContact *contact13=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"];
- KCContact *contact14=[KCContact initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"];
- KCContact *contact15=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"];
- KCContactGroup *group5=[KCContactGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]];
- [_contacts addObject:group5];
- }
- #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;
- }
- @end
- #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];//后面的参数代表更新时的动画
- }
- }


- typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
- UITableViewCellAccessoryNone, // 不显示任何图标
- UITableViewCellAccessoryDisclosureIndicator, // 跳转指示图标
- UITableViewCellAccessoryDetailDisclosureButton, // 内容详情图标和跳转指示图标
- UITableViewCellAccessoryCheckmark, // 勾选图标
- UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // 内容详情图标
- };

- //
- // KCMainViewController.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCMainViewController.h"
- #import "KCContact.h"
- #import "KCContactGroup.h"
- @interface KCMainViewController ()<uitableviewdatasource,uitableviewdelegate,uialertviewdelegate>{
- UITableView *_tableView;
- NSMutableArray *_contacts;//联系人模型
- NSIndexPath *_selectedIndexPath;//当前选中的组和行
- }
- @end
- @implementation KCMainViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //初始化数据
- [self initData];
- //创建一个分组样式的UITableView
- _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
- //设置数据源,注意必须实现对应的UITableViewDataSource协议
- _tableView.dataSource=self;
- //设置代理
- _tableView.delegate=self;
- [self.view addSubview:_tableView];
- }
- #pragma mark 加载数据
- -(void)initData{
- _contacts=[[NSMutableArray alloc]init];
- KCContact *contact1=[KCContact initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"];
- KCContact *contact2=[KCContact initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"];
- KCContactGroup *group1=[KCContactGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]];
- [_contacts addObject:group1];
- KCContact *contact3=[KCContact initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"];
- KCContact *contact4=[KCContact initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"];
- KCContact *contact5=[KCContact initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"];
- KCContactGroup *group2=[KCContactGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]];
- [_contacts addObject:group2];
- KCContact *contact6=[KCContact initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"];
- KCContact *contact7=[KCContact initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"];
- KCContactGroup *group3=[KCContactGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]];
- [_contacts addObject:group3];
- KCContact *contact8=[KCContact initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"];
- KCContact *contact9=[KCContact initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"];
- KCContact *contact10=[KCContact initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"];
- KCContact *contact11=[KCContact initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"];
- KCContact *contact12=[KCContact initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"];
- KCContactGroup *group4=[KCContactGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]];
- [_contacts addObject:group4];
- KCContact *contact13=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"];
- KCContact *contact14=[KCContact initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"];
- KCContact *contact15=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"];
- KCContactGroup *group5=[KCContactGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]];
- [_contacts addObject:group5];
- }
- #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];
- //由于此方法调用十分频繁,cell的标示声明成静态变量有利于性能优化
- static NSString *cellIdentifier=@"UITableViewCellIdentifierKey1";
- static NSString *cellIdentifierForFirstRow=@"UITableViewCellIdentifierKeyWithSwitch";
- //首先根据标示去缓存池取
- UITableViewCell *cell;
- if (indexPath.row==0) {
- cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifierForFirstRow];
- }else{
- cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- }
- //如果缓存池没有取到则重新创建并放到缓存池中
- if(!cell){
- if (indexPath.row==0) {
- cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifierForFirstRow];
- UISwitch *sw=[[UISwitch alloc]init];
- [sw addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
- cell.accessoryView=sw;
- }else{
- cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
- cell.accessoryType=UITableViewCellAccessoryDetailButton;
- }
- }
- if(indexPath.row==0){
- ((UISwitch *)cell.accessoryView).tag=indexPath.section;
- }
- cell.textLabel.text=[contact getName];
- cell.detailTextLabel.text=contact.phoneNumber;
- NSLog(@"cell:%@",cell);
- 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;
- //刷新表格
- NSArray *indexPaths=@[_selectedIndexPath];//需要局部刷新的单元格的组、行
- [_tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];//后面的参数代码更新时的动画
- }
- }
- #pragma mark 重写状态样式方法
- -(UIStatusBarStyle)preferredStatusBarStyle{
- return UIStatusBarStyleLightContent;
- }
- #pragma mark 切换开关转化事件
- -(void)switchValueChange:(UISwitch *)sw{
- NSLog(@"section:%i,switch:%i",sw.tag, sw.on);
- }
- @end





- //
- // KCStatus.h
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import
- @interface KCStatus : NSObject
- #pragma mark - 属性
- @property (nonatomic,assign) long long Id;//微博id
- @property (nonatomic,copy) NSString *profileImageUrl;//头像
- @property (nonatomic,copy) NSString *userName;//发送用户
- @property (nonatomic,assign) NSString *mbtype;//会员类型
- @property (nonatomic,copy) NSString *createdAt;//创建时间
- @property (nonatomic,copy) NSString *source;//设备来源
- @property (nonatomic,copy) NSString *text;//微博内容
- #pragma mark - 方法
- #pragma mark 根据字典初始化微博对象
- -(KCStatus *)initWithDictionary:(NSDictionary *)dic;
- #pragma mark 初始化微博对象(静态方法)
- +(KCStatus *)statusWithDictionary:(NSDictionary *)dic;
- @end
- //
- // KCStatus.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCStatus.h"
- @implementation KCStatus
- #pragma mark 根据字典初始化微博对象
- -(KCStatus *)initWithDictionary:(NSDictionary *)dic{
- if(self=[super init]){
- self.Id=[dic[@"Id"] longLongValue];
- self.profileImageUrl=dic[@"profileImageUrl"];
- self.userName=dic[@"userName"];
- self.mbtype=dic[@"mbtype"];
- self.createdAt=dic[@"createdAt"];
- self.source=dic[@"source"];
- self.text=dic[@"text"];
- }
- return self;
- }
- #pragma mark 初始化微博对象(静态方法)
- +(KCStatus *)statusWithDictionary:(NSDictionary *)dic{
- KCStatus *status=[[KCStatus alloc]initWithDictionary:dic];
- return status;
- }
- -(NSString *)source{
- return [NSString stringWithFormat:@"来自 %@",_source];
- }
- @end
- //
- // KCStatusTableViewCell.h
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import
- @class KCStatus;
- @interface KCStatusTableViewCell : UITableViewCell
- #pragma mark 微博对象
- @property (nonatomic,strong) KCStatus *status;
- #pragma mark 单元格高度
- @property (assign,nonatomic) CGFloat height;
- @end
- //
- // KCStatusTableViewCell.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCStatusTableViewCell.h"
- #import "KCStatus.h"
- #define KCColor(r,g,b) [UIColor colorWithHue:r/255.0 saturation:g/255.0 brightness:b/255.0 alpha:1] //颜色宏定义
- #define kStatusTableViewCellControlSpacing 10 //控件间距
- #define kStatusTableViewCellBackgroundColor KCColor(251,251,251)
- #define kStatusGrayColor KCColor(50,50,50)
- #define kStatusLightGrayColor KCColor(120,120,120)
- #define kStatusTableViewCellAvatarWidth 40 //头像宽度
- #define kStatusTableViewCellAvatarHeight kStatusTableViewCellAvatarWidth
- #define kStatusTableViewCellUserNameFontSize 14
- #define kStatusTableViewCellMbTypeWidth 13 //会员图标宽度
- #define kStatusTableViewCellMbTypeHeight kStatusTableViewCellMbTypeWidth
- #define kStatusTableViewCellCreateAtFontSize 12
- #define kStatusTableViewCellSourceFontSize 12
- #define kStatusTableViewCellTextFontSize 14
- @interface KCStatusTableViewCell(){
- UIImageView *_avatar;//头像
- UIImageView *_mbType;//会员类型
- UILabel *_userName;
- UILabel *_createAt;
- UILabel *_source;
- UILabel *_text;
- }
- @end
- @implementation KCStatusTableViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- [self initSubView];
- }
- return self;
- }
- #pragma mark 初始化视图
- -(void)initSubView{
- //头像控件
- _avatar=[[UIImageView alloc]init];
- [self addSubview:_avatar];
- //用户名
- _userName=[[UILabel alloc]init];
- _userName.textColor=kStatusGrayColor;
- _userName.font=[UIFont systemFontOfSize:kStatusTableViewCellUserNameFontSize];
- [self addSubview:_userName];
- //会员类型
- _mbType=[[UIImageView alloc]init];
- [self addSubview:_mbType];
- //日期
- _createAt=[[UILabel alloc]init];
- _createAt.textColor=kStatusLightGrayColor;
- _createAt.font=[UIFont systemFontOfSize:kStatusTableViewCellCreateAtFontSize];
- [self addSubview:_createAt];
- //设备
- _source=[[UILabel alloc]init];
- _source.textColor=kStatusLightGrayColor;
- _source.font=[UIFont systemFontOfSize:kStatusTableViewCellSourceFontSize];
- [self addSubview:_source];
- //内容
- _text=[[UILabel alloc]init];
- _text.textColor=kStatusGrayColor;
- _text.font=[UIFont systemFontOfSize:kStatusTableViewCellTextFontSize];
- _text.numberOfLines=0;
- // _text.lineBreakMode=NSLineBreakByWordWrapping;
- [self addSubview:_text];
- }
- #pragma mark 设置微博
- -(void)setStatus:(KCStatus *)status{
- //设置头像大小和位置
- CGFloat avatarX=10,avatarY=10;
- CGRect avatarRect=CGRectMake(avatarX, avatarY, kStatusTableViewCellAvatarWidth, kStatusTableViewCellAvatarHeight);
- _avatar.image=[UIImage imageNamed:status.profileImageUrl];
- _avatar.frame=avatarRect;
- //设置会员图标大小和位置
- CGFloat userNameX= CGRectGetMaxX(_avatar.frame)+kStatusTableViewCellControlSpacing ;
- CGFloat userNameY=avatarY;
- //根据文本内容取得文本占用空间大小
- CGSize userNameSize=[status.userName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kStatusTableViewCellUserNameFontSize]}];
- CGRect userNameRect=CGRectMake(userNameX, userNameY, userNameSize.width,userNameSize.height);
- _userName.text=status.userName;
- _userName.frame=userNameRect;
- //设置会员图标大小和位置
- CGFloat mbTypeX=CGRectGetMaxX(_userName.frame)+kStatusTableViewCellControlSpacing;
- CGFloat mbTypeY=avatarY;
- CGRect mbTypeRect=CGRectMake(mbTypeX, mbTypeY, kStatusTableViewCellMbTypeWidth, kStatusTableViewCellMbTypeHeight);
- _mbType.image=[UIImage imageNamed:status.mbtype];
- _mbType.frame=mbTypeRect;
- //设置发布日期大小和位置
- CGSize createAtSize=[status.createdAt sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kStatusTableViewCellCreateAtFontSize]}];
- CGFloat createAtX=userNameX;
- CGFloat createAtY=CGRectGetMaxY(_avatar.frame)-createAtSize.height;
- CGRect createAtRect=CGRectMake(createAtX, createAtY, createAtSize.width, createAtSize.height);
- _createAt.text=status.createdAt;
- _createAt.frame=createAtRect;
- //设置设备信息大小和位置
- CGSize sourceSize=[status.source sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kStatusTableViewCellSourceFontSize]}];
- CGFloat sourceX=CGRectGetMaxX(_createAt.frame)+kStatusTableViewCellControlSpacing;
- CGFloat sourceY=createAtY;
- CGRect sourceRect=CGRectMake(sourceX, sourceY, sourceSize.width,sourceSize.height);
- _source.text=status.source;
- _source.frame=sourceRect;
- //设置微博内容大小和位置
- CGFloat textX=avatarX;
- CGFloat textY=CGRectGetMaxY(_avatar.frame)+kStatusTableViewCellControlSpacing;
- CGFloat textWidth=self.frame.size.width-kStatusTableViewCellControlSpacing*2;
- CGSize textSize=[status.text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kStatusTableViewCellTextFontSize]} context:nil].size;
- CGRect textRect=CGRectMake(textX, textY, textSize.width, textSize.height);
- _text.text=status.text;
- _text.frame=textRect;
- _height=CGRectGetMaxY(_text.frame)+kStatusTableViewCellControlSpacing;
- }
- #pragma mark 重写选择事件,取消选中
- -(void)setSelected:(BOOL)selected animated:(BOOL)animated{
- }
- @end
- //
- // KCCutomCellViewController.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCStatusCellViewController.h"
- #import "KCStatus.h"
- #import "KCStatusTableViewCell.h"
- @interface KCStatusCellViewController ()<uitableviewdatasource,uitableviewdelegate,uialertviewdelegate>{
- UITableView *_tableView;
- NSMutableArray *_status;
- NSMutableArray *_statusCells;//存储cell,用于计算高度
- }
- @end
- @implementation KCStatusCellViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //初始化数据
- [self initData];
- //创建一个分组样式的UITableView
- _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
- //设置数据源,注意必须实现对应的UITableViewDataSource协议
- _tableView.dataSource=self;
- //设置代理
- _tableView.delegate=self;
- [self.view addSubview:_tableView];
- }
- #pragma mark 加载数据
- -(void)initData{
- NSString *path=[[NSBundle mainBundle] pathForResource:@"StatusInfo" ofType:@"plist"];
- NSArray *array=[NSArray arrayWithContentsOfFile:path];
- _status=[[NSMutableArray alloc]init];
- _statusCells=[[NSMutableArray alloc]init];
- [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- [_status addObject:[KCStatus statusWithDictionary:obj]];
- KCStatusTableViewCell *cell=[[KCStatusTableViewCell alloc]init];
- [_statusCells addObject:cell];
- }];
- }
- #pragma mark - 数据源方法
- #pragma mark 返回分组数
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 1;
- }
- #pragma mark 返回每组行数
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return _status.count;
- }
- #pragma mark返回每行的单元格
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *cellIdentifier=@"UITableViewCellIdentifierKey1";
- KCStatusTableViewCell *cell;
- cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- if(!cell){
- cell=[[KCStatusTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
- }
- //在此设置微博,以便重新布局
- KCStatus *status=_status[indexPath.row];
- cell.status=status;
- return cell;
- }
- #pragma mark - 代理方法
- #pragma mark 重新设置单元格高度
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- //KCStatusTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
- KCStatusTableViewCell *cell= _statusCells[indexPath.row];
- cell.status=_status[indexPath.row];
- return cell.height;
- }
- #pragma mark 重写状态样式方法
- -(UIStatusBarStyle)preferredStatusBarStyle{
- return UIStatusBarStyleLightContent;
- }
- @end

- #pragma mark 添加工具栏
- -(void)addToolbar{
- CGRect frame=self.view.frame;
- _toolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, kContactToolbarHeight)];
- // _toolbar.backgroundColor=[UIColor colorWithHue:246/255.0 saturation:246/255.0 brightness:246/255.0 alpha:1];
- [self.view addSubview:_toolbar];
- UIBarButtonItem *removeButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(remove)];
- UIBarButtonItem *flexibleButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
- UIBarButtonItem *addButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
- NSArray *buttonArray=[NSArray arrayWithObjects:removeButton,flexibleButton,addButton, nil];
- _toolbar.items=buttonArray;
- }
- #pragma mark 删除
- -(void)remove{
- //直接通过下面的方法设置编辑状态没有动画
- //_tableView.editing=!_tableView.isEditing;
- [_tableView setEditing:!_tableView.isEditing animated:true];
- }


- #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];
- }

- //
- // KCContactViewController.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCContactViewController.h"
- #import "KCContact.h"
- #import "KCContactGroup.h"
- #define kContactToolbarHeight 44
- @interface KCContactViewController ()<uitableviewdatasource,uitableviewdelegate,uialertviewdelegate>{
- UITableView *_tableView;
- UIToolbar *_toolbar;
- NSMutableArray *_contacts;//联系人模型
- NSIndexPath *_selectedIndexPath;//当前选中的组和行
- BOOL _isInsert;//记录是点击了插入还是删除按钮
- }
- @end
- @implementation KCContactViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //初始化数据
- [self initData];
- //创建一个分组样式的UITableView
- _tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
- _tableView.contentInset=UIEdgeInsetsMake(kContactToolbarHeight, 0, 0, 0);
- [self.view addSubview:_tableView];
- //添加工具栏
- [self addToolbar];
- //设置数据源,注意必须实现对应的UITableViewDataSource协议
- _tableView.dataSource=self;
- //设置代理
- _tableView.delegate=self;
- }
- #pragma mark 加载数据
- -(void)initData{
- _contacts=[[NSMutableArray alloc]init];
- KCContact *contact1=[KCContact initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"];
- KCContact *contact2=[KCContact initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"];
- KCContactGroup *group1=[KCContactGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]];
- [_contacts addObject:group1];
- KCContact *contact3=[KCContact initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"];
- KCContact *contact4=[KCContact initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"];
- KCContact *contact5=[KCContact initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"];
- KCContactGroup *group2=[KCContactGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]];
- [_contacts addObject:group2];
- KCContact *contact6=[KCContact initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"];
- KCContact *contact7=[KCContact initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"];
- KCContactGroup *group3=[KCContactGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]];
- [_contacts addObject:group3];
- KCContact *contact8=[KCContact initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"];
- KCContact *contact9=[KCContact initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"];
- KCContact *contact10=[KCContact initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"];
- KCContact *contact11=[KCContact initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"];
- KCContact *contact12=[KCContact initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"];
- KCContactGroup *group4=[KCContactGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]];
- [_contacts addObject:group4];
- KCContact *contact13=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"];
- KCContact *contact14=[KCContact initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"];
- KCContact *contact15=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"];
- KCContactGroup *group5=[KCContactGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]];
- [_contacts addObject:group5];
- }
- #pragma mark 添加工具栏
- -(void)addToolbar{
- CGRect frame=self.view.frame;
- _toolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, kContactToolbarHeight)];
- // _toolbar.backgroundColor=[UIColor colorWithHue:246/255.0 saturation:246/255.0 brightness:246/255.0 alpha:1];
- [self.view addSubview:_toolbar];
- UIBarButtonItem *removeButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(remove)];
- UIBarButtonItem *flexibleButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
- UIBarButtonItem *addButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
- NSArray *buttonArray=[NSArray arrayWithObjects:removeButton,flexibleButton,addButton, nil];
- _toolbar.items=buttonArray;
- }
- #pragma mark 删除
- -(void)remove{
- //直接通过下面的方法设置编辑状态没有动画
- //_tableView.editing=!_tableView.isEditing;
- _isInsert=false;
- [_tableView setEditing:!_tableView.isEditing animated:true];
- }
- #pragma mark 添加
- -(void)add{
- _isInsert=true;
- [_tableView setEditing:!_tableView.isEditing animated:true];
- }
- #pragma mark - 数据源方法
- #pragma mark 返回分组数
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return _contacts.count;
- }
- #pragma mark 返回每组行数
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- KCContactGroup *group1=_contacts[section];
- return group1.contacts.count;
- }
- #pragma mark返回每行的单元格
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- //NSIndexPath是一个对象,记录了组和行信息
- KCContactGroup *group=_contacts[indexPath.section];
- KCContact *contact=group.contacts[indexPath.row];
- 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;
- return cell;
- }
- #pragma mark - 代理方法
- #pragma mark 设置分组标题
- -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- KCContactGroup *group=_contacts[section];
- return group.name;
- }
- #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];
- [destinationGroup.contacts insertObject:sourceContact atIndex:destinationIndexPath.row];
- if(sourceGroup.contacts.count==0){
- [_contacts removeObject:sourceGroup];
- [tableView reloadData];
- }
- }
- #pragma mark 取得当前操作状态,根据不同的状态左侧出现不同的操作按钮
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
- if (_isInsert) {
- return UITableViewCellEditingStyleInsert;
- }
- return UITableViewCellEditingStyleDelete;
- }
- #pragma mark 重写状态样式方法
- -(UIStatusBarStyle)preferredStatusBarStyle{
- return UIStatusBarStyleLightContent;
- }
- @end
- //
- // KCContactTableViewController.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCContactTableViewController.h"
- #import "KCContact.h"
- #import "KCContactGroup.h"
- #define kSearchbarHeight 44
- @interface KCContactTableViewController (){
- UITableView *_tableView;
- UISearchBar *_searchBar;
- //UISearchDisplayController *_searchDisplayController;
- NSMutableArray *_contacts;//联系人模型
- NSMutableArray *_searchContacts;//符合条件的搜索联系人
- BOOL _isSearching;
- }
- @end
- @implementation KCContactTableViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //初始化数据
- [self initData];
- //添加搜索框
- [self addSearchBar];
- }
- #pragma mark - 数据源方法
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- if (_isSearching) {
- return 1;
- }
- return _contacts.count;;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if (_isSearching) {
- return _searchContacts.count;
- }
- KCContactGroup *group1=_contacts[section];
- return group1.contacts.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- KCContact *contact=nil;
- if (_isSearching) {
- contact=_searchContacts[indexPath.row];
- }else{
- KCContactGroup *group=_contacts[indexPath.section];
- contact=group.contacts[indexPath.row];
- }
- 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;
- return cell;
- }
- #pragma mark - 代理方法
- #pragma mark 设置分组标题
- -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- KCContactGroup *group=_contacts[section];
- return group.name;
- }
- #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 重写状态样式方法
- -(UIStatusBarStyle)preferredStatusBarStyle{
- return UIStatusBarStyleLightContent;
- }
- #pragma mark 加载数据
- -(void)initData{
- _contacts=[[NSMutableArray alloc]init];
- KCContact *contact1=[KCContact initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"];
- KCContact *contact2=[KCContact initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"];
- KCContactGroup *group1=[KCContactGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]];
- [_contacts addObject:group1];
- KCContact *contact3=[KCContact initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"];
- KCContact *contact4=[KCContact initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"];
- KCContact *contact5=[KCContact initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"];
- KCContactGroup *group2=[KCContactGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]];
- [_contacts addObject:group2];
- KCContact *contact6=[KCContact initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"];
- KCContact *contact7=[KCContact initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"];
- KCContactGroup *group3=[KCContactGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]];
- [_contacts addObject:group3];
- KCContact *contact8=[KCContact initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"];
- KCContact *contact9=[KCContact initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"];
- KCContact *contact10=[KCContact initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"];
- KCContact *contact11=[KCContact initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"];
- KCContact *contact12=[KCContact initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"];
- KCContactGroup *group4=[KCContactGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]];
- [_contacts addObject:group4];
- KCContact *contact13=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"];
- KCContact *contact14=[KCContact initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"];
- KCContact *contact15=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"];
- KCContactGroup *group5=[KCContactGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]];
- [_contacts addObject:group5];
- }
- #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;
- }
- @end

- //
- // KCContactTableViewController.m
- // UITableView
- //
- // Created by Kenshin Cui on 14-3-1.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCContactTableViewControllerWithUISearchDisplayController.h"
- #import "KCContact.h"
- #import "KCContactGroup.h"
- #define kSearchbarHeight 44
- @interface KCContactTableViewControllerWithUISearchDisplayController ()<uisearchbardelegate,uisearchdisplaydelegate>{
- UITableView *_tableView;
- UISearchBar *_searchBar;
- UISearchDisplayController *_searchDisplayController;
- NSMutableArray *_contacts;//联系人模型
- NSMutableArray *_searchContacts;//符合条件的搜索联系人
- //BOOL _isSearching;
- }
- @end
- @implementation KCContactTableViewControllerWithUISearchDisplayController
- - (void)viewDidLoad {
- [super viewDidLoad];
- //初始化数据
- [self initData];
- //添加搜索框
- [self addSearchBar];
- }
- #pragma mark - 数据源方法
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- // if (_isSearching) {
- // return 1;
- // }
- //如果当前是UISearchDisplayController内部的tableView则不分组
- if (tableView==self.searchDisplayController.searchResultsTableView) {
- return 1;
- }
- return _contacts.count;;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- // if (_isSearching) {
- // return _searchContacts.count;
- // }
- //如果当前是UISearchDisplayController内部的tableView则使用搜索数据
- if (tableView==self.searchDisplayController.searchResultsTableView) {
- return _searchContacts.count;
- }
- KCContactGroup *group1=_contacts[section];
- return group1.contacts.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- KCContact *contact=nil;
- // if (_isSearching) {
- // contact=_searchContacts[indexPath.row];
- // }else{
- // KCContactGroup *group=_contacts[indexPath.section];
- // contact=group.contacts[indexPath.row];
- // }
- //如果当前是UISearchDisplayController内部的tableView则使用搜索数据
- if (tableView==self.searchDisplayController.searchResultsTableView) {
- contact=_searchContacts[indexPath.row];
- }else{
- KCContactGroup *group=_contacts[indexPath.section];
- contact=group.contacts[indexPath.row];
- }
- 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;
- return cell;
- }
- #pragma mark - 代理方法
- #pragma mark 设置分组标题
- -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
- if (tableView==self.searchDisplayController.searchResultsTableView) {
- return @"搜索结果";
- }
- KCContactGroup *group=_contacts[section];
- return group.name;
- }
- #pragma mark 选中之前
- -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [_searchBar resignFirstResponder];//退出键盘
- return indexPath;
- }
- #pragma mark - 搜索框代理
- //#pragma mark 取消搜索
- //-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
- // //_isSearching=NO;
- // _searchBar.text=@"";
- // //[self.tableView reloadData];
- // [_searchBar resignFirstResponder];
- //}
- //
- //#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 - UISearchDisplayController代理方法
- -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
- [self searchDataWithKeyWord:searchString];
- return YES;
- }
- #pragma mark 重写状态样式方法
- -(UIStatusBarStyle)preferredStatusBarStyle{
- return UIStatusBarStyleLightContent;
- }
- #pragma mark 加载数据
- -(void)initData{
- _contacts=[[NSMutableArray alloc]init];
- KCContact *contact1=[KCContact initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"];
- KCContact *contact2=[KCContact initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"];
- KCContactGroup *group1=[KCContactGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]];
- [_contacts addObject:group1];
- KCContact *contact3=[KCContact initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"];
- KCContact *contact4=[KCContact initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"];
- KCContact *contact5=[KCContact initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"];
- KCContactGroup *group2=[KCContactGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]];
- [_contacts addObject:group2];
- KCContact *contact6=[KCContact initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"];
- KCContact *contact7=[KCContact initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"];
- KCContactGroup *group3=[KCContactGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]];
- [_contacts addObject:group3];
- KCContact *contact8=[KCContact initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"];
- KCContact *contact9=[KCContact initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"];
- KCContact *contact10=[KCContact initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"];
- KCContact *contact11=[KCContact initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"];
- KCContact *contact12=[KCContact initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"];
- KCContactGroup *group4=[KCContactGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]];
- [_contacts addObject:group4];
- KCContact *contact13=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"];
- KCContact *contact14=[KCContact initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"];
- KCContact *contact15=[KCContact initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"];
- KCContactGroup *group5=[KCContactGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]];
- [_contacts addObject:group5];
- }
- #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{
- _searchBar=[[UISearchBar alloc]init];
- [_searchBar sizeToFit];//大小自适应容器
- _searchBar.placeholder=@"Please input key word...";
- _searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
- _searchBar.showsCancelButton=YES;//显示取消按钮
- //添加搜索框到页眉位置
- _searchBar.delegate=self;
- self.tableView.tableHeaderView=_searchBar;
- _searchDisplayController=[[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
- _searchDisplayController.delegate=self;
- _searchDisplayController.searchResultsDataSource=self;
- _searchDisplayController.searchResultsDelegate=self;
- [_searchDisplayController setActive:NO animated:YES];
- }
- @end


UITableView全面解析的更多相关文章
- iOS开发系列--UITableView全面解析
--UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...
- UITableView全面解析,讲的好详细
--UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...
- [转]UITableView全面解析
转自:http://www.cnblogs.com/kenshincui/p/3931948.html#mvc 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软 ...
- 常用几个UITableView,UICollectionView UIScrollView关键点
UITableView UITableView 在Ios中大量使用,我们对UITableview中的有关知识进行整理 UITAbleView是表视图控制器 1 UITableView ...
- iOS开发系列文章(持续更新……)
iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...
- iOS开发系列--网络开发
概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...
- iOS之UI组件整理
作者:神兽gcc 授权本站转载. 最近把iOS里的UI组件重新整理了一遍,简单来看一下常用的组件以及它们的实现.其实现在这些组件都可以通过Storyboard很快的生成,只是要向这些组件能够变得生动起 ...
- iOS开发系列--让你的应用“动”起来
--iOS核心动画 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建 ...
- iOS开发系列--并行开发其实很容易
--多线程开发 概览 大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的, ...
随机推荐
- c#设计模式之简单工厂
1.面向对象的3大属性,封装.继承.多态,以一个加单的计算机为例: 创建一个父类Operation 有两个属性 和一个计算方法(虚方法),便于子类重写: public class Operation ...
- .net获取本机公网IP代码
类的代码如下: using System; using System.Net; using System.Text.RegularExpressions; namespace Keleyi.Com { ...
- asp.net页面后退,重复弹出上一页对话框处理办法
我们在实例中,虽然页面内有导航,但是用户使用的时候难免会使用浏览器的后退按钮. 时常会发现,当后退的时候,上一页的对话框会自动弹出,下面是解决办法. 问题:使用此js代码,后退按钮时,重复显示对话框内 ...
- 【VBS】vbs指定编码保存文本文件(含xml、ini什么的)
本文还是折腾安装包期间衍生出来的产物. 我那安装包在安装期间有这个动作: - 让用户填写一些信息,待安装完成后把这些信息写入软件安装目录中的指定ini.xml文件中 上文说的是如何用vbs写ini,i ...
- IT技术开发人员获得成功的六大步骤
IT技术开发人士成功的6大步骤 一个前辈在移民加拿大后写的文章,写得不错,值得借鉴,转来给大家看看,也给自己 序言:经过001多年的洗礼,认识了这里这么多的JJMMGGDD,前几天刚得到签证, 无 ...
- ASP.NET访问Excel 失败的解决方法(错误号:80070005,8000401a)
用asp.net把值写入Excel在本地测试通过,然后提交服务器后老是写入不成功 并提示错误: Retrieving the COM class factory for component with ...
- Scalaz(11)- Monad:你存在的意义
前面提到了scalaz是个函数式编程(FP)工具库.它提供了许多新的数据类型.拓展的标准类型及完整的一套typeclass来支持scala语言的函数式编程模式.我们知道:对于任何类型,我们只需要实现这 ...
- GJM:用C#实现网络爬虫(二) [转载]
上一篇<用C#实现网络爬虫(一)>我们实现了网络通信的部分,接下来继续讨论爬虫的实现 3. 保存页面文件 这一部分可简单可复杂,如果只要简单地把HTML代码全部保存下来的话,直接存文件就行 ...
- css知多少(1)——我来问你来答
1. 引言 各位前端或者伪前端(比如作者本人)的同志们,css对你们来说不是很陌生.比如我,在几年之前上大学的时候,给外面做网站就用css,而且必须用css.这样算下来也得六年多了,有些功能可能轻车熟 ...
- mongodb系列3 mongo mongoskin 连接以及连接数的问题进阶
1)使用mongodb连接mongo var mongo = require('mongodb'), //引入mongodb dbHost = '127.0.0.1', dbPort = 27017; ...