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];
}

利用的是属性传值。

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















评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值