本文主要实现通讯录的部分功能(分组名、索引、分组的组名)等等功能:
废话不多说了,先上效果图:
在工程中需要导入一个plist文件,文件图如图:
工程目录文件如图:
工程程序如图所示:
RootTableViewController.h
#import <UIKit/UIKit.h> @interface RootTableViewController : UITableViewController @property(strong,nonatomic) NSDictionary *dic; @property(strong,nonatomic) NSArray *arrkeys; @end
RootTableViewController.m
#import "RootTableViewController.h" @interface RootTableViewController () @end @implementation RootTableViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; self.dic=[NSDictionary dictionaryWithContentsOfFile:path]; // NSLog(@"%@",self.dic); // NSLog(@"%@",self.dic.allKeys); // 字典排序 字典的输出是无序的, 需要排序 self.arrkeys=[self.dic.allKeys sortedArrayUsingSelector:@selector(compare:)]; // NSLog(@"%@",self.arrkeys); // 唯一标识符 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - 每个组的数量 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.arrkeys.count; } #pragma mark - 组数列表 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *key=self.arrkeys[section]; NSArray *tempArr=self.dic[key]; // NSLog(@"%@",key); return tempArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath]; NSString *key=self.arrkeys[indexPath.section]; NSArray *tempArr=self.dic[key]; NSString *name=tempArr[indexPath.row]; cell.textLabel.text=name; return cell; } #pragma mark - 返回行高的方法 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } #pragma mark - 系统默认的开头关键字 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return self.arrkeys[section]; // return [NSString stringWithFormat:@"%c",(char)(‘A‘+section)]; } #pragma mark - 自定义自体大小的开头关键字 -(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UILabel *lbl=[[UILabel alloc] init]; lbl.backgroundColor=[UIColor redColor]; lbl.tintColor=[UIColor yellowColor]; lbl.text=self.arrkeys[section]; lbl.font=[UIFont systemFontOfSize:30.0]; return lbl; } #pragma mark - 设置右边索引高度 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 50; } #pragma mark - 设置所有分区的标题的列表 -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { return self.arrkeys; } #pragma mark - 单选控制格式 -(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath { if (indexPath.row==0) { return UITableViewCellAccessoryCheckmark; } else if(indexPath.row==1){ return UITableViewCellAccessoryDetailDisclosureButton; } else if(indexPath.row==2){ return UITableViewCellAccessoryDisclosureIndicator; }else{ return UITableViewCellAccessoryNone; } } ......... @end
注:
通讯录中的各种属性和显示规格都在RootTableViewController.m里设置!!!
AppDelegate.h
#import <UIKit/UIKit.h> #import "RootTableViewController.h" @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
#import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStyleGrouped]]; return YES; } ....... @end
iOS--通讯录(UITableViewController)
原文:http://www.cnblogs.com/bolin-123/p/5293162.html