zoukankan      html  css  js  c++  java
  • 通讯录

      通讯录可实现增删改

    //把导航控制器设置为给视图

    #import "AppDelegate.h"

    #import "ViewController.h"

    @interfaceAppDelegate ()

     

    @end

     

    @implementation AppDelegate

     

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

        [self.window  makeKeyAndVisible];

        UINavigationController *nvc = [[UINavigationControlleralloc]initWithRootViewController:[[ViewControlleralloc]init]];

        self.window.rootViewController = nvc;

        returnYES;

    }

     

    #import <UIKit/UIKit.h>

    #import "AddViewController.h"

    #import "DetailViewController.h"

     

    @interface ViewController : UIViewController<Adddelegate,Editdelegate>

     

     

    @end

     

     

     

    #import "ViewController.h"

    #import "Constant.h"

    #import "MyTableViewCell.h"

    #import "AddViewController.h"

     

    @interfaceViewController ()<UITableViewDataSource,UITableViewDelegate>

     

    {

        UITableView *tabel;

        NSMutableArray *array;  //用于存放数据对象

        NSInteger   index;

     

    }

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

        self.title = @"通讯录";

        tabel = [[UITableViewalloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];//tabel的位置与大小

        

        tabel.delegate = self;

        tabel.dataSource = self;

        tabel.rowHeight = 80;//tabel的行高

        [self.view addSubview:tabel];

        Constant *con = [[Constant alloc]init];

        array = [con initdata];

        

        

        

        UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(addinfo)];

        self.navigationItem.rightBarButtonItem = item;

    }

    -(void) addinfo

    {

        AddViewController *addvc = [[AddViewControlleralloc]init];

        addvc.delegate = self;

        [self.navigationControllerpushViewController:addvc animated:YES];

     

    }

    -(void) addcontact:(Constant *)info

     

    {

        NSLog(@"%@",info.name) ;

        [array addObject:info]; //将新添加的对象放入数组里

        [tabelreloadData];

     

    }

    -(void) editinfo:(Constant *)info5

    {

        [arrayreplaceObjectAtIndex:indexwithObject:info5];//将改过的对象替换原来的

        [tabelreloadData];//数据的更新

     

    }

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

    {

        return  array.count;

     

    }

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

    {

       static NSString *inde = @"cell";

        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inde];

        if (cell == nil) {

            cell = [[MyTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:inde];

        }

        

        Constant *info =array[indexPath.row];

        cell.imageview1.image = [UIImage imageNamed:info.iamge];

        cell.namelabel.text = info.name;

        cell.phontlabel.text = info.phone;

        return cell;

     

    }

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

    {

        returnYES;

     

    }

    //实现tabel上的内容可删除

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

    {

        [array removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:0];//移除被选中的行

     

    }

    -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        DetailViewController *datavc = [[DetailViewControlleralloc]init];

        [self.navigationControllerpushViewController:datavc animated:YES];

        datavc.delegate = self;//设置委托

        datavc.info = array[indexPath.row];//获得数据对象,进行向下一级传值

        index = indexPath.row;

        

     

     

    }

    - (void)didReceiveMemoryWarning {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

     

    @end

     

    #import <Foundation/Foundation.h>

     

    @interface Constant : NSObject

     

    @property (nonatomic,copyNSString *iamge;

     

    @property (nonatomic,copyNSString *name;

     

    @property (nonatomic,copyNSString *phone;

     

    -(NSMutableArray *) initdata;

     

    @end

    #import "Constant.h"

     

    @implementation Constant

     

    -(NSMutableArray *) initdata

    {

        Constant *info1 = [[Constant alloc]init];

        info1.iamge = @"1.jpg";

        info1.name = @"申明康";

        info1.phone = @"13528695827";

        

        Constant *info2 = [[Constant alloc]init];

        info2.iamge = @"15.jpg";

        info2.name = @"赵永杰";

        info2.phone = @"18528645027";

        

        Constant *info3 = [[Constant alloc]init];

        info3.iamge = @"3.jpg";

        info3.name = @"刘贵曾";

        info3.phone = @"13027455776";

        

        Constant *info4 = [[Constant alloc]init];

        info4.iamge = @"4.jpg";

        info4.name = @"方阿端";

        info4.phone = @"13589449369";

     

        NSMutableArray *arr = [NSMutableArray arrayWithObjects:info1,info2,info3,info4, nil];

        return arr;

    }

     

    @end

    #import <UIKit/UIKit.h>

     

    @interface MyTableViewCell : UITableViewCell

     

    @property (nonatomic,strong) UIImageView *imageview1;

     

    @property (nonatomic,strong) UILabel *namelabel;

     

    @property (nonatomic,strong) UILabel *phontlabel;

     

     

    @end

    #import "MyTableViewCell.h"

     

    @implementation MyTableViewCell

     

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

    {

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

        if (self) {

            _imageview1 = [[UIImageViewalloc]initWithFrame:CGRectMake(5, 5, 60, 60)];

            [self addSubview:_imageview1];

            

            

            _namelabel = [[UILabelalloc]initWithFrame:CGRectMake(80, 5, 200, 20)];

            [self addSubview:_namelabel];

            

            

            

            _phontlabel = [[UILabelalloc]initWithFrame:CGRectMake(80, 45, 200, 20)];

            [self addSubview:_phontlabel];

        }

        return  self;

    }

     

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

        [super setSelected:selected animated:animated];

     

        // Configure the view for the selected state

    }

     

    @end

     

    #import <UIKit/UIKit.h>

    #import "Constant.h"

     

    @protocol Adddelegate <NSObject>

     

    -(void) addcontact:(Constant *) info;

     

    @end

     

    @interface AddViewController : UIViewController

     

    @property (nonatomic,weak) id<Adddelegate> delegate;

     

     

    @end

     

    #import "AddViewController.h"

    #import "ViewController.h"

    #import "Constant.h"

     

    @interfaceAddViewController ()

    {

        UITextField *namefile;

        UITextField *phonefile;

     

    }

     

    @end

     

    @implementation AddViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

        self.title = @"新增";

        self.view.backgroundColor = [UIColorwhiteColor];

        

        namefile = [[UITextFieldalloc]initWithFrame:CGRectMake(50, 100, 200, 50)];

        namefile.backgroundColor = [UIColorgrayColor];

        [self.view addSubview:namefile];

        phonefile = [[UITextFieldalloc]initWithFrame:CGRectMake(50, 200, 200, 50)];

         phonefile.backgroundColor = [UIColorgrayColor];

        [self.view addSubview: phonefile];

        

        

       UIBarButtonItem *item = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(done)];

        self.navigationItem.rightBarButtonItem = item;

    }

    -(void) done

    {

        Constant  *con = [[Constant alloc]init];

        con.iamge = @"1.jpg";

        con.name = namefile.text;

        con.phone = phonefile.text;

        [_delegate addcontact:con];

        [self.navigationControllerpopViewControllerAnimated:YES];

     

    }

     

    #import <UIKit/UIKit.h>

    #import "Constant.h"

     

    @protocol Editdelegate <NSObject>

     

    -(void) editinfo:(Constant *) info5;

     

    @end

     

    @interface DetailViewController : UIViewController

     

    @property (nonatomic,weak) id<Editdelegate> delegate;//接收委托的对象,weak可以避免循环引用,还可以用assign

     

    @property (nonatomic,strong) Constant *info;//用于接收上级传过来的数据

     

     

    @end

    #import "DetailViewController.h"

     

    @interfaceDetailViewController ()

    {

        UITextField *namefile;

        UITextField *phonefile;

        UIButton *rightBtn;

        BOOL  isEdit;//判断是否在编辑状态

     

    }

     

    @end

     

    @implementation DetailViewController

     

    - (void)viewDidLoad {

        [superviewDidLoad];

        

        self.title = @"详情";

        self.view.backgroundColor = [UIColorwhiteColor];

    /****************************UITextField的创建**************************/

        namefile = [[UITextFieldalloc] initWithFrame:CGRectMake(40, 80, 200, 30)];

        namefile.borderStyle = UITextBorderStyleRoundedRect;

        namefile.backgroundColor = [UIColorgrayColor];

       namefile.text = _info.name;

        namefile.userInteractionEnabled = NO;

        [self.view addSubview:namefile];

        

        phonefile = [[UITextFieldalloc] initWithFrame:CGRectMake(40, 130, 200, 30)];

        phonefile.borderStyle = UITextBorderStyleRoundedRect;

        phonefile.backgroundColor = [UIColorgrayColor];

        phonefile.userInteractionEnabled = NO;//是否允许与用户交互

        phonefile.text = _info.phone;

        [self.view addSubview:phonefile];

     /****************************UITextField的创建**************************/

        

        

        

        

    /***************************自定义导航栏按钮*****************************/

        rightBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        rightBtn.frame = CGRectMake(0, 0, 80, 40);

        rightBtn.backgroundColor = [UIColorgrayColor];

        [rightBtnsetTitle:@"编辑"forState:UIControlStateNormal];

        [rightBtnaddTarget:selfaction:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

        UIBarButtonItem *item =[[UIBarButtonItemalloc]initWithCustomView:rightBtn];

        self.navigationItem.rightBarButtonItem = item;

    /***************************自定义导航栏按钮*****************************/

    }

    -(void) edit

    {

        if (!isEdit) {

            [rightBtnsetTitle:@"完成"forState:UIControlStateNormal];

            namefile.userInteractionEnabled = YES;

            phonefile.userInteractionEnabled = YES;

            isEdit = YES;

        }

        else{

          

            [rightBtnsetTitle:@"编辑"forState:UIControlStateNormal];

            namefile.userInteractionEnabled = NO;

            phonefile.userInteractionEnabled = NO;

            isEdit = NO;

            Constant *info = [[Constant alloc]init];

            info.name = namefile.text;

            info.phone = phonefile.text;

            info.iamge = @"2.jpg";

            [_delegate editinfo:info];

            [self.navigationControllerpopViewControllerAnimated:YES];

            

        

        

        }

     

     

     

    }

  • 相关阅读:
    vue路由的两种模式,hash与history
    javascript的继承小结
    attr和prop区别
    ie6、7下 text-indent 问题
    推荐链接
    iphone中 input圆角bug
    gif图片加载问题
    IE7中绝对定位元素之间的遮盖问题
    多行文本溢出显示省略号(...)的方法
    ie6兼容之绝对定位元素内容为空时高度问题
  • 原文地址:https://www.cnblogs.com/lcl15/p/4986760.html
Copyright © 2011-2022 走看看