通讯录.数据来自字典

rootviewcontroller.h

 

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

 

@property(nonatomic,retain)NSMutableArray * keys;

@property(nonatomic,retain)NSMutableDictionary *PersonDic;

@property(nonatomic,retain)UITableView * tableView;

@property(nonatomic,retain)UIView * addView;

 

@end

 

rootviewcontroller.m

 

//

//  RootViewController.m

//  AddressBook1

//

//  Created by 有梦想没有什么不可以 on 13-12-17.

//  Copyright (c) 2013 有梦想没有什么不可以. All rights reserved.

//

 

#import "RootViewController.h"

#import "Person.h"

#import "UIButton+CreatNewButton.h"

#import "Creat.h"

#import "PersonCell.h"

 

@interfaceRootViewController ()

{

    Creat * initialview;

    Creat * telephoneview;

    Creat * sexview;

    Creat * nameview;

}

 

@end

 

@implementation RootViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    returnself;

}

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    //创建UITableView

    self.tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0, 64, 320, 416) style:UITableViewStylePlain];

    self.tableView.dataSource = self;        //设置数据源

    self.tableView.delegate = self;      //设置代理

    self.tableView.rowHeight = 40;

    [self.view addSubview:self.tableView];       //添加到跟视图控制器上

    [self.tableView release];        //释放

 

    [selfcreatMenuBarView];

    [selfcreatView];

    [selfinitPersonData];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

//初始化PersonData

-(void)initPersonData

{

   NSString * filePath = [[NSBundle mainBundle]pathForResource:@"PersonData" ofType:@"plist"];      //找到PersonData的路径

    NSDictionary * dic = [NSDictionarydictionaryWithContentsOfFile:filePath];//把文件里面内容存到字典里面

    self.keys = [NSMutableArrayarrayWithArray:[[dic allKeys]sortedArrayUsingSelector:@selector(compare:)] ];

    self.PersonDic = [NSMutableDictionarydictionary];

    for (NSString * key in self.keys) {

        NSArray * arr =[dic objectForKey:key];

        NSMutableArray * group = [NSMutableArray arrayWithCapacity:[arr count]];

        for (NSDictionary *d in arr) {

            Person *p = [[Person alloc]initWithName:[d objectForKey:@"name"] telephone:[d objectForKey:@"phoneNumber"] sex:[d objectForKey:@"sex"]];

            [group addObject:p];

        }

        [self.PersonDic setObject:group forKey:key];

    }

    NSLog(@"%@",self.PersonDic);

}

 

-(void)creatMenuBarView

{

    //创建一个MenuBarView视图

    UIView * menuBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 44)];

    [self.view addSubview:menuBarView];

    [menuBarView release];

    

    //创建编辑(Edit) Button

    UIButton * EditButton = [UIButton buttonWithFrame:CGRectMake(5, 7,50 , 30) titlelabel:@"Edit" font:[UIFont systemFontOfSize:20] target:self action:@selector(edit:) buttontype:UIButtonTypeRoundedRect];

    [EditButton setTitle:@"Done"forState:UIControlStateSelected];

    [menuBarView addSubview:EditButton];

    

    

    //创建添加按钮:

    UIButton * addButton = [UIButtonbuttonWithFrame:CGRectMake(270, 7, 30, 30) titlelabel:nilfont:niltarget:selfaction:@selector(add:) buttontype:UIButtonTypeContactAdd];

    [menuBarView addSubview: addButton];

}

#pragma mark -

#pragma mark Button

//实现button的事件

//edit事件

-(void)edit:(UIButton *)button

{

    button.selected = !button.selected;

    if ([button.titleLabel.text isEqualToString:@"Edit"]) {

        [self.tableViewsetEditing:YESanimated:YES];

    }else {

        [self.tableViewsetEditing:NOanimated:YES];

 

    }

}

//Add事件

-(void)add:(UIButton *)button

{

    self.addView.hidden = NO;

}

 

 

#pragma mark -

#pragma mark UITabelViewDatasource

//数据源里面的方法:

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    NSString * str = [self.keys objectAtIndex:section];

    NSArray * arr =[self.PersonDic objectForKey:str];

    return [arr count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    PersonCell * cell = [tableView dequeueReusableCellWithIdentifier:@"wahaha"];

    if (cell == nil) {

        cell = [[PersonCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:@"wahaha"];

    }

    NSString *key = [self.keys objectAtIndex:indexPath.section];

    NSArray * arr = [self.PersonDic objectForKey:key];

    Person * p = [arr objectAtIndex:indexPath.row];

    cell.nameLable.text =p.name;

    cell.telLable.text = p.telephone;

    cell.sexLable.text = p.sex;

    

    

    

    

    return  cell;

}

//设置行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 60.0;

}

 

 

//设置有多少分段

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [self.keys count];

}

 

//设置分段头

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return [self.keys objectAtIndex:section];

}

 

//设置索引

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    returnself.keys;

}

 

#pragma mark -

#pragma mark Editing

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    returnYES;

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

    returnUITableViewCellEditingStyleDelete;

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView beginUpdates];

 

    NSString * str = [self.keys objectAtIndex:indexPath.section];

    [[self.PersonDicobjectForKey:str] removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:[NSArrayarrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];

    

   //删分段头

    NSMutableArray * arr =[NSMutableArrayarrayWithArray:[self.PersonDicobjectForKey:str]] ;

    if ([arr count] == 0) {

        NSIndexSet * set = [[[NSIndexSet alloc]initWithIndex:indexPath.section] autorelease];

        [tableView deleteSections:set withRowAnimation:UITableViewRowAnimationLeft];

        [self.keys removeObjectAtIndex:indexPath.section];

    }

    

    [tableView endUpdates];

}

 

#pragma mark -

#pragma mark Moving

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return  YES;

}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

    NSMutableArray * arr = [NSMutableArray arrayWithArray:[self.PersonDic objectForKey: [self.keys objectAtIndex:sourceIndexPath.section]]];

    NSString * str = [arr objectAtIndex:sourceIndexPath.row];

    [arr insertObject:str atIndex:destinationIndexPath.row];

}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

    if (proposedDestinationIndexPath.section == sourceIndexPath.section) {

        return proposedDestinationIndexPath;

    }

    return sourceIndexPath;

}

#pragma mark -

#pragma mark AddView

 

-(void)creatView

{

    self.addView = [[UIViewalloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    [self.view addSubview:self.addView];

    self.addView.tag = 1000;

    self.addView.hidden = YES;

    self.addView.backgroundColor = [UIColorredColor];

    [self.addView release];

    

    //创建一个MenuBarView视图

    UIView * menuBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 44)];

    [self.addView addSubview:menuBarView];

    [menuBarView release];

    

    //创建返回 Button

    UIButton * EditButton = [UIButton buttonWithFrame:CGRectMake(20, 7,70 , 30) titlelabel:@"返回" font:[UIFont systemFontOfSize:20] target:self action:@selector(returnView:) buttontype:UIButtonTypeRoundedRect];

    [menuBarView addSubview:EditButton];

    

    

    //创建保存按钮:

    UIButton * addButton = [UIButton buttonWithFrame:CGRectMake(230, 7, 70, 30) titlelabel:@"保存" font:nil target:self action:@selector(save:) buttontype:UIButtonTypeRoundedRect];

    [menuBarView addSubview: addButton];

    

    nameview = [[Creat alloc]initWithFram:CGRectMake(10, 100, 260, 40) description:@"姓名:"];

    [self.addViewaddSubview:nameview];

    [nameview release];

    

    sexview = [[Creat alloc]initWithFram:CGRectMake(10, 170, 260, 40) description:@"性别:"];

    [self.addViewaddSubview:sexview];

    [sexview release];

    

    telephoneview = [[Creat alloc]initWithFram:CGRectMake(10, 240, 260, 40) description:@"手机:"];

    [self.addViewaddSubview:telephoneview];

    [telephoneviewrelease];

    

    initialview = [[Creat alloc]initWithFram:CGRectMake(10, 310, 260, 40) description:@"首字母:"];

    [self.addViewaddSubview:initialview];

    [initialviewrelease];

}

-(void)returnView:(UIButton *)button

{

    self.addView.hidden = YES;

}

 

-(void)save:(UIButton *)button

{

    NSString * key = initialview.text;

    if ([self.keys containsObject:key]) {

        Person * p = [[Personalloc]initWithName:nameview.texttelephone:telephoneview.textsex:sexview.text];

        NSMutableArray * arr = [NSMutableArray arrayWithArray:[self.PersonDic objectForKey:key]];

        NSLog(@"%@",arr);

        [arr insertObject:p atIndex:0];

        [self.PersonDic setValue:arr forKey:key];

    }else{

        [self.keys addObject:initialview.text];

        self.keys = [NSMutableArrayarrayWithArray:[self.keyssortedArrayUsingSelector:@selector(compare:)] ];

        NSMutableArray *arr1 = [NSMutableArray arrayWithArray:[self.PersonDic objectForKey:key]] ;

        Person * p = [[Personalloc]initWithName:nameview.texttelephone:telephoneview.textsex:sexview.text];

        [arr1 insertObject:p atIndex:0];

        [self.PersonDic setValue:arr1 forKey:key];

 

    }

    

    [self.tableViewreloadData];

    self.addView.hidden = YES;

}

 

//正确方法

//arr = [NSArray alloc]initWithObjects:@"asd",@"asd", nil];

//dealloc [arr release];

 

//错误方法

//arr = [NSArray arrayWithObjects:@"qwe",@"asd", nil];

 

//总结:实例变量使用初始化方法,不能使用便利构造器(不知道何时autorelease);

//总结:属性使用便利构造器的方法,不能使用初始化方法(内存泄露, retain引用计数为2);

@end

 

 

personcell.h

 

#import <UIKit/UIKit.h>

 

@interface PersonCell : UITableViewCell

 

@property(nonatomic,retain)UILabel * nameLable;

@property(nonatomic,retain)UILabel * telLable;

@property(nonatomic,retain)UILabel * sexLable;

 

 

.m

#import "PersonCell.h"

 

@implementation PersonCell

 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        _nameLable  = [[UILabelalloc]initWithFrame:CGRectMake(10, 5, 100, 30)];

        [self.contentView addSubview:_nameLable];

        _sexLable  = [[UILabelalloc]initWithFrame:CGRectMake(10, 35, 50, 15)];

        _sexLable.font = [UIFont systemFontOfSize:12];

        [self.contentView addSubview:_sexLable];

        _telLable  = [[UILabelalloc]initWithFrame:CGRectMake(130, 5, 140, 45)];

        [self.contentView addSubview:_telLable];

                // Initialization code

    }

    returnself;

}

 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [super setSelected:selected animated:animated];

 

    // Configure the view for the selected state

}

 

 

 

 

posted @ 2013-12-22 22:05  徐坤很无聊  阅读(428)  评论(0编辑  收藏  举报