本文主要實現通訊錄的部分功能(分組名、索引、分組的組名)等等功能: 廢話不多說了,先上效果圖: 在工程中需要導入一個plist文件,文件圖如圖: 工程目錄文件如圖: 工程程式如圖所示: RootTableViewController.h RootTableViewController.m 註: 通訊
本文主要實現通訊錄的部分功能(分組名、索引、分組的組名)等等功能:
廢話不多說了,先上效果圖:
在工程中需要導入一個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