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;
    }
  • 相关阅读:
    微软外服 AlI In One
    js 循环多次和循环一次的时间的性能对比 All In One
    vue inject All In One
    Excel 表格数据倒置 All In One
    SVG tickets All In One
    OH MY ZSH All In One
    js array for loop performance compare All In One
    mac terminal show You have new mail All In one
    新闻视频 26 制作母版页
    转自牛腩 母版页和相对路径
  • 原文地址:https://www.cnblogs.com/zhaochaobin/p/5293795.html
Copyright © 2011-2022 走看看