iOS--通訊錄(UITableViewController),uitableview通訊錄

來源:互聯網
上載者:User

iOS--通訊錄(UITableViewController),uitableview通訊錄

本文主要實現通訊錄的部分功能(分組名、索引、分組的組名)等等功能:

廢話不多說了,先上:

 

在工程中需要匯入一個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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.