UI_表視圖編輯(刪除,移動,插入)

#import "AppDelegate.h"

#import "RootViewController.h"

#import "TableViewController.h"

#import "TwoTableViewController.h"

#import "TestTableViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    

//    RootViewController *rootVC = [RootViewController new];

//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];

//    self.window.rootViewController = navC;

   

    

//    TableViewController *tableVC = [[TableViewController alloc]init];

//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:tableVC];

//    self.window.rootViewController = navC;

    

    

    TwoTableViewController *twoVC = [TwoTableViewController new];    

    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:twoVC];

    self.window.rootViewController = navC;

    

    

//    TestTableViewController *testVC = [TestTableViewController new];

//    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:testVC];

//    self.window.rootViewController = navC;

    

    

    

    

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}








#import "RootViewController.h"


@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,retain)NSMutableArray *allDataMutableArray;  //存儲數據

@end


@implementation RootViewController

//懶加載

-(NSMutableArray *)allDataMutableArray{

    if (!_allDataMutableArray) {

        _allDataMutableArray = [NSMutableArray new];

    }

    return _allDataMutableArray;

}


//造假數據

-(void)createData{

    [self.allDataMutableArray addObject:@"HanOBa"];

    [self.allDataMutableArray addObject:@"Hello"];

    [self.allDataMutableArray addObject:@"World"];

    [self.allDataMutableArray addObject:@"添加一行"];

}





- (void)viewDidLoad {

    [super viewDidLoad];

//表視圖編輯

    self.navigationItem.title = @"表視圖編輯";

    

//新建一個表視圖和屏幕大小一致

    UITableView *myTableView = [[UITableView alloc]initWithFrame:self.view.bounds];

    myTableView.tag = 1000;

//指定代理並實現必須實現的代理方法

    myTableView.delegate = self;

    myTableView.dataSource = self;

    

//將表視圖添加到父視圖上

    [self.view addSubview:myTableView];

    

//註冊單元格

    [myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];

    

//調用假數據

    [self createData];

    

//系統提供的一個編輯按鈕(右按鈕--刪除)

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

//自己編輯一個左按鈕(左按鈕--刷新)

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(leftAction)];

}


//實現左按鈕的方法:

-(void)leftAction{

    UITableView *tableView = (UITableView *)[self.view viewWithTag:1000];

    //刷新tableView上面的數據,實際上就是讓實現的代理方法重新執行一次

    [tableView reloadData];

}











#pragma mark - 必須實現的方法!!!  分區的行數和分區數不能給死,要給數據值,變量!!!

//定義單元格

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

    //從重用隊列中獲取一個cell,註冊單元格時候給的重用標示符是什麼,這裏的重用標示符就是什麼!!!

    UITableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];

    mycell.textLabel.text = self.allDataMutableArray[indexPath.row];

    

    return mycell;

}

//每個分區的行數

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

    

    return self.allDataMutableArray.count;

}




#pragma mark - 編輯、修改等的代理方法

//系統提供的開啓當前視圖控制器的編輯模式

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{


    [super setEditing:editing animated:animated];

    

    //將表示圖的編輯狀態開啓

    UITableView *myTableView = (UITableView *)[self.view viewWithTag:1000];

    [myTableView setEditing:editing animated:animated];

    

}

//設置編輯樣式

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


//編輯,插入

    //return UITableViewCellEditingStyleInsert;

//刪除

    //return UITableViewCellEditingStyleDelete;

//如果是最後一行,就讓編輯模式爲插入,其他行都是刪除

    if (indexPath.row == self.allDataMutableArray.count-1) {

        return UITableViewCellEditingStyleInsert;

    }else{

        return UITableViewCellEditingStyleDelete;

    }

}


//編輯狀態完成之後執行的代理方法(//刪除或者插入操作完成所執行的代理方法)

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

    //刪除的編輯樣式

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //先刪除數據源中的數據

        [self.allDataMutableArray removeObjectAtIndex:indexPath.row];

        //刪除單元格

            //刪除某一位置的單元格

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

    }else if(editingStyle ==UITableViewCellEditingStyleInsert){

        //插入數據源

        [self.allDataMutableArray insertObject:@"Fuck" atIndex:indexPath.row];

        //插入單元格

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

    

    }

}


//設置移動狀態

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


    return YES;// 這樣是讓全部可以移動,還可以實現讓某一行可以移動,用indexPath.row就行

}

//移動完成之後執行的代理方法

    //sourceIndexPath:單元格移動之前所在的位置

    //destinationIndexPath:單元格移動完成之後所在的位置

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

    //先得到移動單元格上面的數據

    NSString *dataStr = [self.allDataMutableArray objectAtIndex:sourceIndexPath.row];

    //把數組中原位置的數據刪除掉

    [self.allDataMutableArray removeObjectAtIndex:sourceIndexPath.row];

    //把保存的原位置的數據插入到單元格所在的新位置

    [self.allDataMutableArray insertObject:dataStr atIndex:destinationIndexPath.row];

}



//檢測移動過程的代理方法

    //sourceIndexPath:單元格原來所在的位置

    //proposedDestinationIndexPath:將要移動到的位置

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

    

    if (sourceIndexPath.row == self.allDataMutableArray.count-1) {

        return sourceIndexPath;

    }else if(proposedDestinationIndexPath.row == self.allDataMutableArray.count-1){

        return sourceIndexPath;

    }else{

        return proposedDestinationIndexPath;

    }

    

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end








//自帶一個表視圖控制器!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#import "TableViewController.h"


@interface TableViewController ()

@property(nonatomic,retain)NSMutableArray *Array;   //數組


@end


@implementation TableViewController

//懶加載

-(NSMutableArray *)Array{

    if (!_Array) {

        _Array = [[NSMutableArray alloc]init];

    }

    return _Array;

}


//假數據

-(void)data{

    [self.Array addObject:@"AAAAA"];

    [self.Array addObject:@"BBBBB"];

    [self.Array addObject:@"CCCCC"];

    [self.Array addObject:@"DDDDD"];

    [self.Array addObject:@"EEEEE"];

    [self.Array addObject:@"插入"];

}



- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title = @"馬勒戈壁臥槽";

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

//調用假數據

    [self data];

    

    /**

     *  創建表視圖

        協議

        顯示到父視圖上面

        註冊單元格

        不需要創建tabelView!直接使用就ok

     */

//4步驟

    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];


    self.tableView.delegate = self;

    self.tableView.dataSource = self;

    

    

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];

    self.tableView.tag = 10000;


    

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    //取消註釋以下行保存報告之間的選擇。

    //自我。clearsSelectionOnViewWillAppear =沒有;

    //取消註釋以下行來顯示一個編輯按鈕在導航欄視圖控制器。

    // self.navigationItemrightBarButtonItem = self.editButtonItem;

//右邊按鈕

     self.navigationItem.rightBarButtonItem = self.editButtonItem;

}


#pragma mark - Table view data source

//分區的個數

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Potentially incomplete method implementation.#警告不完整的方法實現。

    // Return the number of sections.

    return 1;

}



//行數

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

#warning Incomplete method implementation.#警告不完整的方法實現。

    // Return the number of rows in the section.

    return self.Array.count;

}


//創建單元格

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];

    cell.textLabel.text = self.Array[indexPath.row];


    return cell;

}




// Override to support conditional editing of the table view.

    //覆蓋,支持有條件的編輯的表格視圖。

//確定是否處於編輯狀態

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

    

    // Return NO if you do not want the specified item to be editable.

        //此函數是控制某行或者其他的可不可以編輯

    

    return YES;

}

//控制某行的狀態,比如刪除或者添加

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

    if (indexPath.row == self.Array.count-1) {

        return UITableViewCellEditingStyleInsert;

    }else{

        return UITableViewCellEditingStyleDelete;

    }

   //我寫的代碼的意思是,讓最後一行爲插入模式,其他的行都爲刪除模式

    

}



// Override to support editing the table view.

    //支持編輯表格視圖重寫

//編輯狀態下,進行刪除,添加的方法:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source

            //從數據源中刪除行

        [self.Array removeObjectAtIndex:indexPath.row];

        

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

            //創建一個新的合適的類的實例,將其插入到數組,並添加一個新行表視圖

        //插入數據源

        [self.Array insertObject:@"FFFFF" atIndex:indexPath.row];

        //插入單元格

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

    }   

}



//移動方法

// Override to support rearranging the table view.

    //覆蓋支持重新整理表格視圖

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

}




// Override to support conditional rearranging of the table view.

    //覆蓋表視圖的支持有條件的重新安排

//移動結束執行方法

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

    // Return NO if you do not want the item to be re-orderable.

        //如果你不想要項目re-orderable返回NO

    return YES;

}



/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

    //storyboard-based應用程序中,您通常會希望導航之前做一些準備-(void)prepareForSegue:(UIStoryboardSegue *)segue發送方:發送者(id){/ /得到新的視圖控制器使用[segue destinationViewController]/ /選中的對象傳遞給新視圖控制器。


@end







#import "TwoTableViewController.h"


@interface TwoTableViewController ()

//創建一個字典,承載所有分區聯繫人

@property(nonatomic,retain)NSMutableDictionary *allDataMutableDic;

@property(nonatomic,retain)NSMutableArray *allKeysArray;

@end


@implementation TwoTableViewController

//懶加載   !!!感嘆號不能少!!!

-(NSMutableDictionary *)allDataMutableDic{

    if (!_allDataMutableDic) {

        _allDataMutableDic = [NSMutableDictionary new];

    }

    return _allDataMutableDic;

}


-(NSMutableArray *)allKeysArray{

     if (!_allKeysArray) {

          _allKeysArray = [NSMutableArray new];

     }

     return _allKeysArray;

}



//造假數據

-(void)createData{

    NSDictionary *person_1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"HanOBa",@"name",@"18",@"age",nil];

    NSDictionary *person_2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Money",@"name",@"19",@"age",nil];

    NSDictionary *person_3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Fuck",@"name",@"20",@"age",nil];

    NSDictionary *person_4 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Love",@"name",@"21",@"age",nil];

    NSDictionary *person_5 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Code",@"name",@"22",@"age",nil];

    NSDictionary *person_6 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Girl",@"name",@"23",@"age",nil];

    NSDictionary *person_7 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Boy",@"name",@"24",@"age",nil];

    NSDictionary *person_8 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Adidas",@"name",@"25",@"age",nil];

    NSDictionary *person_9 = [[NSDictionary alloc]initWithObjectsAndKeys:@"You",@"name",@"26",@"age",nil];

    NSDictionary *person_10 = [[NSDictionary alloc]initWithObjectsAndKeys:@"Nike",@"name",@"27",@"age",nil];

   //將姓名首字母相同的放進同一個可變數組

    NSArray *allPersonArray = [NSArray arrayWithObjects:person_1,person_2,person_3,person_4,person_5,person_6,person_7,person_8,person_9,person_10, nil];

    for (int i  = 0; i<allPersonArray.count; i++) {

        //取出數組中的聯繫人字典

        NSDictionary *Dic = allPersonArray[i];

        //獲取聯繫人首字母

        NSString *oneName = [Dic objectForKey:@"name"];

        NSString *upOne = [self transFormWithSourceString:oneName];

        //根據Key值獲得大字典中的小數組,如果未獲取到,那麼就創建一個可變數組添加,如果獲取到就直接添加。

        if (![self.allDataMutableDic objectForKey:upOne]) {//此分區不存在,需要創建

            NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:Dic, nil];

            [self.allDataMutableDic setObject:mutableArray forKey:upOne];

        }else{//分區存在,直接添加

            NSMutableArray *mutableArray = [self.allDataMutableDic objectForKey:upOne];

            [mutableArray addObject:Dic];

        }

    }

}




//漢字轉拼音

-(NSString *)transFormWithSourceString:(NSString *)sourceStr{


        //將源字符串轉換爲可變字符串

    NSMutableString *ms = [NSMutableString stringWithString:sourceStr];

    if (ms.length) {

        CFStringTransform((__bridge CFMutableStringRef)ms, NULL, kCFStringTransformToLatin,NO);

        //去掉重音

        CFStringTransform((__bridge CFMutableStringRef)ms, NULL, kCFStringTransformStripDiacritics,NO);

            //所有字母大寫

        NSString *upStr = [ms uppercaseString];

            //截取首字母

        NSString *oneStr = [upStr substringToIndex:1];

        return oneStr;

    }

        //將漢子轉換爲帶音調的拼音

            //__bridge作用:只轉換類型,不改變內存管理。

            //第一個參數:需要轉換的漢字,需要可變字符串類型。

            //第二個參數:轉換的範圍,如果全部轉換,直接賦值爲NULL或者0

            //第三個參數:轉換方式,如果是非字母轉換爲字母,直接轉換爲拉丁文

            //第四個參數:轉換過程是否可逆,YES,轉換完成後,返回原字符串,NO:返回轉            換完的字符串

    

    return @"#";

}





- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];

    self.navigationItem.title = @"HanOBa";

    

//    NSString *testStr = [self transFormWithSourceString:@"我是 HanOBa"];

//    NSLog(@"%@",testStr);

    

//創建假數據

    [self createData];

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

    

//註冊單元格

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];

    

//右按鈕

     self.navigationItem.rightBarButtonItem = self.editButtonItem;

     

     [self sort];

     

//如何從資源庫讀取plist文件

     // 獲得文件路徑

          //[NSBundle mainBundle]:所指代的就是工程左側欄所有的文件資源

          //resource:我們所要讀取的文件名稱

          //type:文件類型,也就是文件的後綴名

     NSString *listPath = [[NSBundle mainBundle]pathForResource:@"contactinfo" ofType:@"plist"];

     NSLog(@"路徑-----------%@",listPath);

     //從該文件路徑下讀取字典

     NSDictionary *Dic = [[NSDictionary alloc]initWithContentsOfFile:listPath];

     NSLog(@"-----%@",Dic);

     //讀取圖片

     //[UIImage imageWithContentsOfFile:<#(NSString *)#>];這種不會造成內存

     

     //[UIImage imageNamed:@""];這種會造成短暫內存

     

     NSString *lisPath = [[NSBundle mainBundle]pathForResource:@"Info" ofType:@"plist"];

     NSLog(@"系統的infi路徑------%@",lisPath);

     

}




//冒泡排序

- (void)sort{

     self.allKeysArray = [NSMutableArray arrayWithArray:self.allDataMutableDic.allKeys];

     for (int i = 0; i< self.allKeysArray.count-1; i++) {

          for (int j = 0; j < self.allKeysArray.count-1-i; j++) {

               if ([self.allKeysArray[j] compare:self.allKeysArray[j+1]] > 0) {

                    //                [self.allKeysArray exchangeObjectAtIndex:j withObjectAtIndex:j+1];

                    NSString *temp = self.allKeysArray[j];

                    self.allKeysArray[j] = self.allKeysArray[j+1];

                    self.allKeysArray[j+1] = temp;

               }

          }

     }

}




- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Potentially incomplete method implementation.

    // Return the number of sections.

    return self.allDataMutableDic.count;

}


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

#warning Incomplete method implementation.

    // Return the number of rows in the section.

    

    //先取出大字典中的可變小數組

    NSArray *array = [self.allDataMutableDic.allValues objectAtIndex:section];

    return array.count;

}


//分區標題

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

    //先得到分區的標題

    NSString *title = [self.allKeysArray objectAtIndex:section];

    

    

    return title;

}



//單元格

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];

     //先取出對應位置的鍵

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

    //先取出大字典中的小可變數組

    NSArray *array = [self.allDataMutableDic objectForKey:key];

    //從數組中取出單個聯繫人的信息

    NSDictionary *dic = [array objectAtIndex:indexPath.row];

    

    cell.textLabel.text =[dic objectForKey:@"name"];

    

    

    return cell;

}

//開啓編輯樣式


-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

    [super setEditing:editing animated:animated];



    [self.tableView setEditing:editing animated:animated];


}



//編輯樣式

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


    if (indexPath.row ==self.allDataMutableDic.allValues.count-1) {

        return UITableViewCellEditingStyleInsert;

    }else{

        return UITableViewCellEditingStyleDelete;

    }

}



//編輯完成之後的方法

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

         NSMutableArray *array = [self.allDataMutableDic.allValues objectAtIndex:indexPath.section];

        //先刪除數據源中的數據

        [array removeObjectAtIndex:indexPath.row];

        //刪除某一位置的單元格

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];

    }

}


//右邊索引條

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

     return self.allKeysArray;

}

    







#import "TestTableViewController.h"

#import "contactinfo.h"

@interface TestTableViewController ()

@property(nonatomic,retain)NSDictionary *allPersonDic;  //讀取聯繫人的字典

@end


@implementation TestTableViewController


//plist文件中讀取內容

-(void)readContactFromPlist{

    //文件路徑

    NSString *path = [[NSBundle mainBundle]pathForResource:@"contactinfo" ofType:@"plist"];

    //從路徑下讀取文件

    self.allPersonDic = [[NSDictionary alloc]initWithContentsOfFile:path];

    

    //大字典中所存儲的小數組

    NSArray *array = [self.allPersonDic objectForKey:@"H"];

    //從數組中讀取出單個聯繫人的字典

    NSDictionary *dic = [array objectAtIndex:0];

    //字典快速轉模型

    contactinfo *ContactInfo =[contactinfo new];

    //通過KVC的方式遍歷字典爲模型屬性賦值

    [ContactInfo setValuesForKeysWithDictionary:dic];

    NSLog(@"name---%@",ContactInfo.name_1);

    NSLog(@"age---%@",ContactInfo.age_1);

    NSLog(@"sex---%@",ContactInfo.sex_1);

}




- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.title = @"練習";

    [self readContactFromPlist];

    

    

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Potentially incomplete method implementation.

    // Return the number of sections.

    return 0;

}


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

#warning Incomplete method implementation.

    // Return the number of rows in the section.

    return 0;

}






#import <Foundation/Foundation.h>


@interface contactinfo : NSObject

@property(nonatomic,retain)NSString *ID;    //身份證號

@property(nonatomic,retain)NSString *name_1;  //姓名

@property(nonatomic,assign)NSNumber *age_1;   //年齡

@property(nonatomic,retain)NSString *sex_1;   //性別


@end







#import "contactinfo.h"


@implementation contactinfo


//當我們的字典鍵和模型的屬性名稱不一致,或者字典的鍵的個數多於我們的模型的屬性的個數的時候纔會執行此方法,在此方法中我們可以得到不匹配或者多出的字典中的鍵值對

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{

    NSLog(@"value---%@,key---%@",value,key);

        //處理字典的鍵和模型屬性不匹配的情況

    if ([key isEqualToString:@"name"]) {

        self.name_1 = value;

    }if ([key isEqualToString:@"age"]) {

        self.age_1 = value;

    }if ([key isEqualToString:@"sex"]) {

        self.sex_1 = value;

    }

    

}




@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章