本文轉自:http://www.cocoachina.com/ios/20140922/9710.html 在iOS開發中UITableView可以說是使用最廣泛的控制項,我們平時使用的軟體中到處都可以看到它的影子,類似於微信、QQ、新浪微博等軟體基本上隨處都是UITableView。當然它的廣泛使用
本文轉自:http://www.cocoachina.com/ios/20140922/9710.html
在iOS開發中UITableView可以說是使用最廣泛的控制項,我們平時使用的軟體中到處都可以看到它的影子,類似於微信、QQ、新浪微博等軟體基本上隨處都是UITableView。當然它的廣泛使用自然離不開它強大的功能,今天這篇文章將針對UITableView重點展開討論。今天的主要內容包括:
1.基本介紹 2.數據源 3.代理 4.性能優化 5.UITableViewCell 6.常用操作 7.UITableViewController 8.MVC模式 基本介紹 UITableView有兩種風格:UITableViewStylePlain和UITableViewStyleGrouped。這兩者操作起來其實並沒有本質區別,只是後者按分組樣式顯示前者按照普通樣式顯示而已。大家先看一下兩者的應用: 1>分組樣式 2>不分組樣式 大家可以看到在UITableView中數據只有行的概念,並沒有列的概念,因為在手機操作系統中顯示多列是不利於操作的。UITableView中每行數據都是一個UITableViewCell,在這個控制項中為了顯示更多的信息,iOS已經在其內部設置好了多個子控制項以供開發者使用。如果我們查看UITableViewCell的聲明文件可以發現在內部有一個UIView控制項(contentView,作為其他元素的父控制項)、兩個UILable控制項(textLabel、detailTextLabel)、一個UIImage控制項(imageView),分別用於容器、顯示內容、詳情和圖片。使用效果類似於微信、QQ信息列表: 當然,這些子控制項並不一定要全部使用,具體操作時可以通過UITableViewCellStyle進行設置,具體每個枚舉表示的意思已經在代碼中進行了註釋:- 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:@"Wi