plist文件存儲

1、根據(jù)plist文件生成tableView的靜態(tài)單元格并保存開關狀態(tài)的效果圖

Untitled.gif

2、plist文件示意圖

Snip20161107_3.png
Snip20161107_4.png

3、plist文件存儲的實現(xiàn)步驟

3.1手動創(chuàng)建plist文件

Snip20161107_5.png

*** 說明:plist文件(屬性列表)是一種XML格式的文件,拓展名為plist;如果對象是NSString、NSDictionary、NSArray、NSData、NSNumber等類型,就可以使用writeToFile:atomically:方法直接將對象寫到屬性列表文件中***

3.2給plist文件添加內(nèi)容作為tableView的數(shù)據(jù)源數(shù)組

Snip20161107_9.png

3.3從plist文件中讀取數(shù)據(jù)的核心代碼

-(NSArray *)groups{
    if (_groups ==nil) {
        // 1.獲取文件路徑
        NSString *filePath = [[NSBundle mainBundle] pathForResource:self.plistName ofType:@"plist"];
        _groups = [NSArray arrayWithContentsOfFile:filePath];
    }
    return _groups;
}

選中單元格cell跳轉到下一個控制器
// 選中cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary *group = self.groups[indexPath.section];
   
    NSArray *items = group[@"items"];
    // 獲取每行cell的信息
    NSDictionary *item = items[indexPath.row];
    
    NSString *vcString = item[@"targetVC"];
    
    if (vcString && vcString.length >0 ) {
        // 根據(jù)字符串創(chuàng)建和字符串名字相同的類
        Class cla = NSClassFromString(vcString);
        UIViewController *targetVC = [[cla alloc]init];
        if ([targetVC isKindOfClass:[CLSettingViewController class]]) {
            CLSettingViewController *settingVC  = (CLSettingViewController * ) targetVC;
            settingVC.plistName = item[@"plistName"];
        }
        // 設置控制器的標題
        targetVC.title = item[@"title"];
        
        [self.navigationController pushViewController:targetVC animated:YES];
    }
}

3.4使用偏好設置NSUserDefaults保存開關的狀態(tài)

// 重寫setter方法
- (void)setItem:(NSDictionary *)item{

    _item = item ;
    // 設置cell的title
   self.textLabel.text = item[@"title"];
    
    //設置cell的圖片
    self.imageView.image = [UIImage imageNamed:item[@"icon"]];
    
    NSString * imageView = item[@"accessoryView"];//@"UIImageView"
    
    //根據(jù)字符串轉化類
    Class cla = NSClassFromString(imageView);// UIImageView 類
    
    //根據(jù)類創(chuàng)建類的對象
    id obj = [[cla alloc] init];//創(chuàng)建類的對象
    
    if([obj isKindOfClass:[UIImageView class]])
    {
        UIImageView * imageView = (UIImageView *)obj;
        
        imageView.image = [UIImage imageNamed:@"arrow_right"];
        
        //圖片框和圖片大小一樣
        [imageView sizeToFit];
    }else if ([obj isKindOfClass:[UISwitch class] ]){
    
        UISwitch *mySwitch = (UISwitch *)obj;
        // 從偏好設置獲取值
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   
        mySwitch.on = [userDefaults boolForKey:self.item[@"switchKey"]];
        // 給開關添加事件
        [mySwitch addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
    }
    
    //添加輔助view
    self.accessoryView = obj;

}
// 開關的點擊事件
-(void)valueChanged:(UISwitch *)sender{

    //存儲到偏好設置
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    
    //設置值
    [userDefaults setBool:sender.isOn forKey:self.item[@"switchKey"]];
    
    [userDefaults synchronize];
}

3.5代碼創(chuàng)建plist文件


#import "CLContact.h"

@implementation CLContact
// 編碼 使用kvc進行編碼
- (void)encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
}
// 解碼 從字符串獲取對象
- ( instancetype)initWithCoder:(NSCoder *)aDecoder{

    if (self == [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name" ];
        self.phone = [aDecoder decodeObjectForKey:@"phone"];
    }
    return self;
}
@end
//  ViewController.m
//  代碼創(chuàng)建plist存儲數(shù)據(jù)
//
//  Created by JackChen on 2016/11/7.
//  Copyright ? 2016年 chenlin. All rights reserved.
//

#import "ViewController.h"
#import "CLContact.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self test];
  // 沙盒document文件路徑
  NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
   // document中存儲的plist文件的全路徑
    NSString *filePath = [docPath stringByAppendingPathComponent:@"contact.plist"];
    
    CLContact *contact = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

    NSLog(@"%@----%@",contact.name,contact.phone);
}
-(void)test{
    CLContact *contact = [[CLContact alloc] init];
    contact.name = @"liudehua";
    contact.phone = @"1234";
    NSString *docDir= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [docDir stringByAppendingPathComponent:@"contact.plist"];
    
    [NSKeyedArchiver archiveRootObject:contact  toFile:filePath];    
    NSLog(@"%@",filePath);
}
@end

3.6代碼運行效果

Snip20161107_11.png

*** command + shift + G打開finder前往文件夾 輸入文件路徑 打開plist文件 ***

Snip20161107_12.png
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容