博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通讯录.数据来自字典
阅读量:5331 次
发布时间:2019-06-14

本文共 10515 字,大约阅读时间需要 35 分钟。

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

}

 

 

 

 

转载于:https://www.cnblogs.com/xukunhenwuliao/p/3486626.html

你可能感兴趣的文章
1201 网页基础--JavaScript(DOM)
查看>>
组合数学 UVa 11538 Chess Queen
查看>>
oracle job
查看>>
Redis常用命令
查看>>
XML学习笔记(二)-- DTD格式规范
查看>>
IOS开发学习笔记026-UITableView的使用
查看>>
[转载]电脑小绝技
查看>>
windos系统定时执行批处理文件(bat文件)
查看>>
thinkphp如何实现伪静态
查看>>
BZOJ 2243: [SDOI2011]染色( 树链剖分 )
查看>>
BZOJ 1925: [Sdoi2010]地精部落( dp )
查看>>
c++中的string常用函数用法总结!
查看>>
界面交互之支付宝生活圈pk微信朋友圈
查看>>
[DLX精确覆盖+打表] hdu 2518 Dominoes
查看>>
SuperMap iServerJava 6R扩展领域开发及压力测试---判断点在那个面内(1)
查看>>
Week03-面向对象入门
查看>>
一个控制台程序,模拟机器人对话
查看>>
web.xml 中加载顺序
查看>>
pycharm激活地址
查看>>
hdu 1207 四柱汉诺塔
查看>>