UI开发----简单通讯录的实现

// Created By 郭仔  2015年04月23日23:18:08

// ===================================

简单通讯录的实现:

// ===================================

实现分组展?示学员通讯录。

1、使?用tableView展?示学员通讯录,根据联系?人分组使?用分区显?示联系 ?人,分区header显?示分组名(A~Z)。并提供分区索引。

2、使?用UITableViewCell展?示联系?人头像、姓名、联系?方式。

3、选中联系?人进?入详情?页?面,显?示联系?人信息(头像、姓名、性别、年

龄、联系?方式、爱好)。

4、定义Model类(Student)。使?用ContactList.plist提供数据源。

实现状态(有些烂):

图片和名字弄的有些烂,主要是小功能都实现了。

主要是根据UITableView来实现。

数据在plist文件中存放,把数据取出封装到Student中:

NSDictionary * dic = [[NSDictionary alloc]initWithContentsOfFile:@"/Users/lanou3g/Desktop/Lesson/通讯录练习/通讯录练习/Contact List.plist"];
        NSMutableDictionary *contactDic = [NSMutableDictionary dictionary];
        self.contactDic = contactDic;
        for (NSString * key in dic) {
            NSArray * arr = [dic objectForKey:key];
            NSMutableArray * contactsArray = [NSMutableArray array ];
            for (NSDictionary * pDic in arr) {
                Person * p = [[Person alloc]init];
                p.name = [pDic objectForKey:@"name"];
                p.sex = [pDic objectForKey:@"sex"];
                p.photo = [pDic objectForKey:@"photo"];
                p.age = [[pDic objectForKey:@"age"] intValue];
                [contactsArray addObject:p];
                [p release];
            }
            [contactDic setObject:contactsArray forKey:key];

        }

一个分区中的行数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString * key = [[self.contactDic allKeys] objectAtIndex:section];
    long count = [[self.contactDic objectForKey:key] count];
    return count;
}

通讯录中的分区数:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.contactDic allKeys] count];
}

利用重用机制创建cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"GuoZai" ];
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"GuoZai"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSArray * arr = getPersons(indexPath,self.contactDic);
    Person * per = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = per.name;
    cell.detailTextLabel.text = per.sex;
    cell.imageView.image = [UIImage imageNamed:per.photo];
    return cell;
}

我自己封装的方法,取出字典中Student个数:

NSArray* getPersons(NSIndexPath *indexPath,NSMutableDictionary * dic)
{
    NSString * key = [[dic allKeys] objectAtIndex:indexPath.section];
    NSArray * arr = [dic objectForKey:key];
    return arr;
}

区标题:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[self.contactDic allKeys] objectAtIndex:section];
}

编辑状态:

- (void)rightBtnClick:(UIBarButtonItem *)btn
{
    [self.tableView setEditing:YES animated:YES];
}

可以编辑的行:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.section && !indexPath.row) {
        return NO;
    }
    return YES;
}

编辑格式:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.section && indexPath.row == 1) {
        return UITableViewCellEditingStyleDelete;
    }
    else
        return UITableViewCellEditingStyleInsert;
}

编辑完成:移动数据

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray * arr = (NSMutableArray *)getPersons(indexPath, _contactDic);
        [arr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        Person * p = [[Person alloc]init];
        p.name = @"郭仔";
        p.age = 25;
        p.sex = @"男";
        NSMutableArray * arr = (NSMutableArray *)getPersons(indexPath, _contactDic);
        [arr insertObject:p atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

移动的三个步骤:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSMutableArray * oldArr = (NSMutableArray *)getPersons(sourceIndexPath, self.contactDic);
    Person * p = [[oldArr objectAtIndex:sourceIndexPath.row] retain];
    [oldArr removeObjectAtIndex:sourceIndexPath.row];
   // [tableView deleteRowsAtIndexPaths:@[sourceIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    NSMutableArray * newArr = (NSMutableArray *)getPersons(destinationIndexPath, self.contactDic);
    [newArr insertObject:p atIndex:destinationIndexPath.row];
    [p release];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }
    else
        return sourceIndexPath;
}

页面跳转,查看详情页:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ThirdViewController * thirdVC = [[ThirdViewController alloc]init];
    NSMutableArray * arr = (NSMutableArray * )getPersons(indexPath, _contactDic);
    thirdVC.per = [arr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC release];
}

利用的是属性传值。

一些内存释放的问题自己考虑把。

时间: 2024-08-28 10:03:39

UI开发----简单通讯录的实现的相关文章

iOS开发UI篇—简单介绍静态单元格的使用

iOS开发UI篇-简单介绍静态单元格的使用 一.实现效果与说明 说明:观察上面的展示效果,可以发现整个界面是由一个tableview来展示的,上面的数据都是固定的,且几乎不会改变. 要完成上面的效果,有几种方法: (1)可以直接利用代码,返回三组,在判断每组有多少行,展示些什么数据,这样写"死"的代码建议绝不要使用. (2)稍微灵活一些的,可以把plist文件一懒加载的方式,加载到程序中,动态获取.但是观察界面结构,很容易看出这样需要进行模型嵌套,很麻烦. (3)storyboard提

文顶顶 iOS开发UI篇—简单的浏览器查看程序

文顶顶 iOS开发UI篇—简单的浏览器查看程序 iOS开发UI篇—简单的浏览器查看程序 一.程序实现要求 1.要求 2. 界面分析 (1) 需要读取或修改属性的控件需要设置属性 序号标签 图片 图片描述 左边按钮 右边按钮 (2) 需要监听响应事件的对象,需要添加监听方法 左边按钮 右边按钮 二.实现基本功能的程序 1 // 2 // YYViewController.m 3 // 03-图片浏览器初步 4 // 5 // Created by apple on 14-5-21. 6 // Co

iOS开发UI基础—简单的浏览器查看程序

iOS开发UI基础-简单的浏览器查看程序 一.程序实现要求 1.要求 2. 界面分析 (1) 需要读取或修改属性的控件需要设置属性 序号标签 图片 图片描述 左边按钮 右边按钮 (2) 需要监听响应事件的对象,需要添加监听方法 左边按钮 右边按钮 二.实现基本功能的程序 1 // 2 // YYViewController.m 3 // 03-图片浏览器初步 4 // 5 // Created by apple on 14-5-21. 6 // Copyright (c) 2014年 itcas

Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)

Android UI开发: 横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载) POSTED ON 2014年6月27日 BY 天边的星星 本文内容: 1.横向ListView的所有实现思路; 2.其中一个最通用的思路HorizontalListView,并基于横向ListView开发一个简单的相册: 3.实现的横向ListView在点击.浏览时item背景会变色,并解决了listview里setSelected造成item的选择状态混乱的问题.

基于TINY4412的Andorid开发-------简单的LED灯控制【转】

本文转载自:http://www.cnblogs.com/pengdonglin137/p/3857724.html 基于TINY4412的Andorid开发-------简单的LED灯控制 阅读目录(Content) 一.编写驱动程序 二.编写代码测试驱动程序 三.编写HAL代码 四.编写Framework代码 五.编写JNI代码 六.编写App 参考资料: <Andriod系统源代码情景分析> <嵌入式Linux系统开发完全手册_基于4412_上册> 作者:彭东林 邮箱:[em

Android:日常学习笔记(8)———探究UI开发(5)

Android:日常学习笔记(8)---探究UI开发(5) ListView控件的使用 ListView的简单用法 public class MainActivity extends AppCompatActivity { private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","

WeX5的简单介绍及UI的简单讲解

WeX5的简单介绍及UI的简单讲解 (2016-01-13 14:49:05) 标签: it 分类: WeX5的初步自学 一.WeX5的简单讲解 1.WeX5是前端快速开发框架,可开发跨端运行应用.是移动App/微信/WebApp开发利器,一次开发多平台运行. 二.WeX5平台了解 1.菜单和工具栏 :查看API:启动Tomcat:搜索:首选项:复位透视图 2.透视图 :导入java项目:使用svn 3.模型资源 :文件对比 :新建.复制.删除:重命名文刷件新及文件夹:模型编译 切换到资源管理器

十二、Android UI开发专题(转)

http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=18736&page=1#pid89255Android UI开发专题(一) 之界面设计 近期很多网友对Android用户界面的设计表示很感兴趣,对于Android UI开发自绘控件和游戏制作而言掌握好绘图基础是必不可少的.本次专题分10节来讲述,有关OpenGL ES相关的可能将放到以后再透露.本次主要涉及以下四个包的相关内容: android.content.res 资源类 android.gra

Android UI开发神兵利器之Angrytools

最近很多人在问我,个人App开发者如何去设计UI. 其实这是个人开发者最头痛的问题,搞技术的人,确实没法做到面面俱到,不可能花大量的时间去切图,去做原型设计,去做美工. 当然,虽然我们设计不出那么复杂,精巧的UI,但是简单的东西,我们在没有美工的基础上,通过一些手段,也是可以做的不错的,从本文开始,我们将介绍一些关于Android界面开发的神兵利器,正是这些大神们开发的工具,让Coder也能做出一些不是那么见不得人的设计. Angrytools,我们今天的主角,我想当初作者也是被UI弄的Angr