zoukankan      html  css  js  c++  java
  • ios通讯录的建立

         很多时候我们会用到诸如通讯录,联系人等的建立,通过字母就能够查找到,今天我就来给大家简单的分享下:

         通讯录界面如下所示:

         

        当然首先得导入plist文件

        1.建一个类RootTableViewController(当然这个类名可以随便取,继承与UITableViewController,在AppDelegate.h中导入

    RootTableViewController.h文件。然后在AppDelegate.m文件中把RootTableViewController作为根视图,代码如下:

     1 #import "AppDelegate.h"
     2 
     3 @interface AppDelegate ()
     4 
     5 @end
     6 
     7 @implementation AppDelegate
     8 
     9 
    10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    11     self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStyleGrouped]];
    12 
    13     return YES;
    14 }

    2.然后在RootTableViewController.h文件中定义两个属性

    #import <UIKit/UIKit.h>
    
    @interface RootTableViewController : UITableViewController
    @property(strong,nonatomic) NSDictionary *dic;
    @property(strong,nonatomic) NSArray *arrayKeys;
    
    @end

    3.RootTableViewController.m实现如下代码

    #import "RootTableViewController.h"
    
    @interface RootTableViewController ()
    
    @end
    
    @implementation RootTableViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title=@"通讯录";
        NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames 2" ofType:@"plist"];
        self.dic=[NSDictionary dictionaryWithContentsOfFile:path];
    //    NSLog(@"%@",self.dic.allKeys);
        //字符串排序
        self.arrayKeys=[self.dic.allKeys sortedArrayUsingSelector:@selector(compare:)];
        NSLog(@"%@",self.arrayKeys);
        
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Incomplete implementation, return the number of sections
        return self.arrayKeys.count;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //#warning Incomplete implementation, return the number of rows
        
        NSString *key= self.arrayKeys[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.arrayKeys[indexPath.section];
        NSArray *tempArr=self.dic[key];
        NSString *name=tempArr[indexPath.row];
        cell.textLabel.text=name;
        return cell;
    }
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return [NSString stringWithFormat:@"%c",'A'+(int)section];
    }
    //设置所有分区标题的列表
    -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        return self.arrayKeys;
    }
    
    - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    {
           return @"结尾";
    }
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UILabel *lab=[[UILabel alloc] init];
    //    lab.backgroundColor=[UIColor redColor];
        lab.text=self.arrayKeys[section];
        lab.font=[UIFont systemFontOfSize:30];
        return lab;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 50;
    }
  • 相关阅读:
    Android和PHP开发最佳实践
    python3+pyqt5 +eric5安装配置
    用Python为iOS和Android写跨平台的应用
    No compatible targets were found Do you wish to a add new Android Virtual Device ?
    doc命令大全(详细版)
    安装Qt5.9
    ADT Bundle下载和安装
    阶段性学习内容
    DDMS files not found
    五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程
  • 原文地址:https://www.cnblogs.com/zhaochaobin/p/5293795.html
Copyright © 2011-2022 走看看