• 浅谈ios设计之使用表格UITableVIew设计通讯录的方法


    思路;首先自作一份或者网上下载一份通讯录的   plist   文件
    之后创建工程和显示通讯录的表格
     
     
    代码实现如下(全部代码)
     
    1、创建导航栏和根视图
    AppDelegate.h
    #import <UIKit/UIKit.h>

    #import "SortednameTableViewController.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 {
       //创建根视图(将SortednameTableViewController作为根视图)(UINavigationController 用于创建导航栏)
        self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[SortednameTableViewController alloc]initWithStyle:UITableViewStyleGrouped]];
        return YES;
    }
    2、表格部分(就是创建的类 继承UITableViewController)
    SortednameTableViewController.h

    #import <UIKit/UIKit.h>

    @interface SortednameTableViewController : UITableViewController
    //接收文件的字典
    @property(strong,nonatomic)NSDictionary *Dictionary;
    //接收字典关键字(A~Z)的集合
    @property(strong,nonatomic)NSArray *KeysArray;
    @end
     
    SortednameTableViewController.m

    #import "SortednameTableViewController.h"

    @interface SortednameTableViewController ()

    @end

    @implementation SortednameTableViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
      //设置视图背景色
        self.view.backgroundColor=[UIColor yellowColor];
        //页面标题
        self.title=@"通讯录";
       
        //导入存通讯录的plist文件的路径
        NSString  *path=[[NSBundle mainBundle]pathForResource:@"sortednames" ofType:@"plist"];
        //接收通讯录文件
        self.Dictionary=[NSDictionary dictionaryWithContentsOfFile:path];
        //对字典中的关键字进行排序(选择器排序法)
        self.KeysArray=[self.Dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
       
        //重用表格唯一标识
        [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.KeysArray.count;//关键字的数量
    }
    //每个分区的行数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //#warning Incomplete implementation, return the number of rows
        NSString *key=self.KeysArray[section];
        NSArray *arr=self.Dictionary[key];//获取关键字对应的Value值
       
       
        return arr.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
        NSString *key=self.KeysArray[indexPath.section];//分区的关键字
        NSArray *keyArray=self.Dictionary[key];//关键字对应的value值
        cell.textLabel.text=key;
        cell.textLabel.text=keyArray[indexPath.row];
        cell.textLabel.textAlignment=NSTextAlignmentCenter;//对齐方式
        cell.textLabel.textColor=[UIColor blueColor];//字体颜色
       
        return cell;
    }

    //设置分区标题
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UILabel *label=[[UILabel alloc]init];
        label.backgroundColor=[UIColor redColor];
        label.text=self.KeysArray[section];//分区标题为分区对应的关键字
        label.font=[UIFont systemFontOfSize:30];
        label.textAlignment=NSTextAlignmentCenter;
        return label;
    }
    -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        return self.KeysArray;//显示(A~Z)

    }
    //设置每个分区开头
    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        NSString *str=self.KeysArray[section];
        return str;
    }
    //设置每个分区结尾标题
    -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    {
       return @"end";
    }

    //设置分区高度
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
       
        return 50;
    }
     
    3、效果图 
     
     
  • 相关阅读:
    《ORANGE'S一个操作系统的实现》第7章 TTY与键盘输入的关系。
    《将博客搬至CSDN》
    x86汇编分页模式实验 --《ORANGE'S一个操作系统的实现》中 pmtest8.asm解析
    LR(1)语法分析器生成器(生成Action表和Goto表)java实现(二)
    LR(1)语法分析器生成器(生成Action表和Goto表)java实现(一)
    LeetCode 85. 冗余连接 II
    2397
    机器学习入门(九)之----logistic回归(牛顿法)
    机器学习入门(八)之----logistic回归(番外篇感知机算法)
    机器学习入门(七)之----logistic回归(回归函数与概率模型)
  • 原文地址:https://www.cnblogs.com/guiyangxueyuan/p/5293068.html
Copyright © 2020-2023  润新知