plist文件的创建和读取

课程要点:plist文件的新建与读取给UITableView设置变化的值单元格的删除、插入及刷新      plist文件的新建与读取 

 新建plist  Commadn+N,iOS->Resouce->Property List          


   plist文件还有另外一种展现形式  右键plist文件,open as->Property List就是上面显示的这种方式来展现, open as->source Code 就是下面这种方式来展现

       这种形式下我可以通过改变代码或者增加代码给plist修改或增加数据。  读取plist数据  为了咱们的数据统一,我把我的plist里的代码留下。

      复制代码DataArrayage16name王五name张三age18name李四age19age16name赵六复制代码    读取plist文件分两步    1、找到plist文件在boundle中的路径,    2、使用字典读取该文件        boundles:iOS的应用都是通过bundle进行封装的,对应的bundle类型是Application类型,平时我们通过XCode编译出来的Target(即我们开发的应用),其实就是一个Application类型bundle,即一个文件夹!


   运行后控制台输出:








  给UITableView设置变化的值





 源代码出处: http://www.cnblogs.com/g-ios/p/5049011.html





//  ViewController.m//  Plist文件以及cell的增加与删除////  Created by 王立广 on 15/12/14.//  Copyright © 2015年 王立广. All rights reserved.//#import "ViewController.h"@interface ViewController (){    //UITableView的数据源    NSMutableArray *dataArray;        UITableView *_tableView;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        //获取到plist文件路径    NSString *path = [[NSBundle mainBundle] pathForResource:@"DataPlist" ofType:@"plist"];    //通过字典获取到plist文件中的字典    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];        NSLog(@"数据=%@",dict);        //将plist文件中的数组赋值给工程中的数据源数组    dataArray = [dict[@"DataArray"] mutableCopy];        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 414, 480) style:UITableViewStylePlain];        _tableView.delegate = self;    _tableView.dataSource = self;        _tableView.backgroundColor = [UIColor grayColor];        [self.view addSubview:_tableView];        }//给tableView设置行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{        //数据源数组里面有几个元素,里面就有几行    return dataArray.count;}//每次将要显示cell,就会调用这个方法,在这个方法内设置一个cell并返回就会就将cell放到tableView上面,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        /*    * 这个方法会传进来两个参数          * tableView:就是咱们正在操作的tableView        * indexPath:这个值有两个属性section和row,分别标明这个cell位于第几段第几行          */        static NSString *cellID = @"cellID";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];        if (cell == nil) {                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];    }        cell.backgroundColor = [UIColor yellowColor];            //利用传进来的indexpath参数,来分别取数组里相应的字典,在通过key值得到咱们真正需要的数据    cell.textLabel.text = dataArray[indexPath.row][@"name"];    cell.detailTextLabel.text = [dataArray[indexPath.row][@"age"] stringValue];        return cell;    }@end复制代码单元格的删除、插入及刷新UITableView有两种模式1、正常模式2、编辑模式  1)插入状态  2)删除状态左边是正常模式,中间是编辑模式下的删除状态,只要点击红色的减号,单元格右边就会出现删除按钮,如第三张图片所示。        1、获取到tableView编译模式,tableView是UITableView的一个对象tableView.iSEditing 2、给tableVIew设置编辑模式[tableView setEditing:YES animated:YES];3、只要进入编辑模式,系统就会自动调用这个方法来询问是要进入编辑模式下的删除状态还是插入状态。如果不实现这个方法,系统默认是删除状态。复制代码- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{        //这两种你可以都试试,看一下效果,但最终要选择删除模式,    return UITableViewCellEditingStyleDelete;//    return UITableViewCellEditingStyleInsert;    }复制代码  4、进入编辑模式后,通过上面的代理方法进入相应的状态,此时不管你点击的是删除按钮还是插入按钮,都会进入下面这个方法。实现这个方法直接右划也可以出现右边的删除按钮复制代码- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{        /*        如果点击的是删除按钮,editingStyle参数就是UITableViewCellEditingStyleDelete            如果点击的是插入按钮,editingStyle参数就是UITableViewCellEditingStyleInsert            indexPath:能够获得你点击是哪一行哪一段。              */        //通过判断你点击的是删除还是插入做不同的操作    if (editingStyle == UITableViewCellEditingStyleDelete) {        //删除dataArray中相应cell的数据        [dataArray removeObjectAtIndex:indexPath.row];        //删除cell        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];    }else if (editingStyle == UITableViewCellEditingStyleInsert){        NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];        tempDict[@"name"] = @"小闪";        tempDict[@"age"] = @(18);        //在数据源数组中增加一个字典          [dataArray insertObject:tempDict atIndex:indexPath.row];        //插入一个cell          [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];    }        }复制代码完整代码如下:复制代码////  ViewController.m//  Plist文件以及cell的增加与删除////  Created by 王立广 on 15/12/14.//  Copyright © 2015年 王立广. All rights reserved.//#import "ViewController.h"@interface ViewController (){    //UITableView的数据源    NSMutableArray *dataArray;        UITableView *_tableView;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        //获取到plist文件路径    NSString *path = [[NSBundle mainBundle] pathForResource:@"DataPlist" ofType:@"plist"];        //通过字典获取到plist文件中的字典    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];        NSLog(@"数据=%@",dict[@"DataArray"][1][@"name"]);        //将plist文件中的数组赋值给工程中的数据源数组    dataArray = [dict[@"DataArray"] mutableCopy];            _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 414, 480) style:UITableViewStylePlain];        _tableView.delegate = self;    _tableView.dataSource = self;        _tableView.backgroundColor = [UIColor grayColor];        [self.view addSubview:_tableView];            //在底部添加toolBar    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 680, 414, 56)];        toolBar.backgroundColor = [UIColor grayColor];        [self.view addSubview:toolBar];        UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trash)];        //在toolBar上面加一个删除按钮    toolBar.items = @[trash];    }- (void)trash{        //tableView.iSEditing获得tableView是否属于编辑模式,通过取反来改变tableView的编辑模式    BOOL kbool = !_tableView.isEditing;        // tableView setEditing给tableView设置编辑模式    [_tableView setEditing:kbool animated:YES];}//只要进入编辑模式,系统就会自动调用这个方法来询问是要进入编辑模式下的删除状态还是插入状态。如果不实现这个方法,系统默认是删除状态。- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{        //这两种你可以都试试,看一下效果    return UITableViewCellEditingStyleDelete;//    return UITableViewCellEditingStyleInsert;    }//进入编辑模式后,通过上面的代理方法进入相应的状态,此时不管你点击的是删除按钮还是插入按钮,都会进入下面这个方法。实现这个方法直接右划也可以出现右边的删除按钮- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{        /*        如果点击的是删除按钮,editingStyle参数就是UITableViewCellEditingStyleDelete            如果点击的是插入按钮,editingStyle参数就是UITableViewCellEditingStyleInsert            indexPath:能够获得你点击是哪一行哪一段。              */        //通过判断你点击的是删除还是插入做不同的操作    if (editingStyle == UITableViewCellEditingStyleDelete) {        //删除dataArray中相应cell的数据        [dataArray removeObjectAtIndex:indexPath.row];                  //删除cell时,没有执行cellForRowAtIndexPath方法        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];    }else if (editingStyle == UITableViewCellEditingStyleInsert){                NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];        tempDict[@"name"] = @"小闪";        tempDict[@"age"] = @(18);      //在数据源数组中增加一个字典      [dataArray insertObject:tempDict atIndex:indexPath.row];          //插入一个cell      [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];  }        }//给tableView设置行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    //数据源数组里面有几个元素,里面就有几行    return dataArray.count;}//每次将要显示cell,就会调用这个方法,在这个方法内设置一个cell并返回就会就将cell放到tableView上面,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        //用static修饰的变量只会初始化一次。    static NSString *cellID = @"cellID";        /*    * 这个方法会传进来两个参数          * tableView:就是咱们正在操作的tableView          * indexPath:这个值有两个属性section和row,分别标明这个cell位于第几段第几行          */        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];        if (cell == nil) {                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];    }        cell.backgroundColor = [UIColor yellowColor];    cell.textLabel.text = dataArray[indexPath.row][@"name"];    cell.detailTextLabel.text = [dataArray[indexPath.row][@"age"] stringValue];        return cell;    }@end复制代码PS:咱们在删除cell的同时也在数组中删除了cell所对应的数据。并没有删除plist文件中的内容。 单元格的刷新 改动有三: 1、将存放plist数据的字典设置为全局变量 2、在toolBar上新增了一个refresh按钮 3、在refresh按钮的触发方法里,重新给数据源数组赋值,然后使用UITableView的reloadData方法刷新表格。  PS:reloadData刷新的意思是tableView的所有代理方法重走。重新计算多少行,重新设置cell等等。。。。复制代码////  ViewController.m//  Plist文件以及cell的增加与删除////  Created by 王立广 on 15/12/14.//  Copyright © 2015年 王立广. All rights reserved.//#import "ViewController.h"@interface ViewController (){

//UITableView的数据源

NSMutableArray *dataArray;

UITableView *_tableView;

//从plist文件中取出来的字典

NSDictionary *dict;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//获取到plist文件路径

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

//通过字典获取到plist文件中的字典

dict = [[NSDictionary alloc]initWithContentsOfFile:path];

NSLog(@"数据=%@",dict[@"DataArray"][1][@"name"]);

//将plist文件中的数组赋值给工程中的数据源数组

dataArray = [dict[@"DataArray"] mutableCopy];

_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 414, 480) style:UITableViewStylePlain];

_tableView.delegate = self;

_tableView.dataSource = self;

_tableView.backgroundColor = [UIColor grayColor];

[self.view addSubview:_tableView];

//在底部添加toolBar

UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 680, 414, 56)];

toolBar.backgroundColor = [UIColor grayColor];

[self.view addSubview:toolBar];

UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trash)];

UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];

//在toolBar上面加一个删除按钮

toolBar.items = @[trash,refresh];

}

- (void)refresh{

dataArray = [dict[@"DataArray"] mutableCopy];

[_tableView reloadData];

}

- (void)trash{

//tableView.iSEditing获得tableView是否属于编辑模式,通过取反来改变tableView的编辑模式

BOOL kbool = !_tableView.isEditing;

// tableView setEditing给tableView设置编辑模式

[_tableView setEditing:kbool animated:YES];

}

//只要进入编辑模式,系统就会自动调用这个方法来询问是要进入编辑模式下的删除状态还是插入状态。如果不实现这个方法,系统默认是删除状态。

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

//这两种你可以都试试,看一下效果,

return UITableViewCellEditingStyleDelete;

//    return UITableViewCellEditingStyleInsert;

}

//进入编辑模式后,通过上面的代理方法进入相应的状态,此时不管你点击的是删除按钮还是插入按钮,都会进入下面这个方法。实现这个方法直接右划也可以出现右边的删除按钮

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

/*

如果点击的是删除按钮,editingStyle参数就是UITableViewCellEditingStyleDelete

如果点击的是插入按钮,editingStyle参数就是UITableViewCellEditingStyleInsert

indexPath:能够获得你点击是哪一行哪一段。

*/

//通过判断你点击的是删除还是插入做不同的操作

if (editingStyle == UITableViewCellEditingStyleDelete) {

//删除dataArray中相应cell的数据

[dataArray removeObjectAtIndex:indexPath.row];

//删除cell时,没有执行cellForRowAtIndexPath方法

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

}else if (editingStyle == UITableViewCellEditingStyleInsert){

NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];

tempDict[@"name"] = @"小闪";

tempDict[@"age"] = @(18);

//在数据源数组中增加一个字典

[dataArray insertObject:tempDict atIndex:indexPath.row];

//插入一个cell

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

}

}

//给tableView设置行数

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

//数据源数组里面有几个元素,里面就有几行

return dataArray.count;

}

//每次将要显示cell,就会调用这个方法,在这个方法内设置一个cell并返回就会就将cell放到tableView上面,

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

//用static修饰的变量只会初始化一次。

static NSString *cellID = @"cellID";

/*

* 这个方法会传进来两个参数

* tableView:就是咱们正在操作的tableView

* indexPath:这个值有两个属性section和row,分别标明这个cell位于第几段第几行

*/

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

}

cell.backgroundColor = [UIColor yellowColor];

cell.textLabel.text = dataArray[indexPath.row][@"name"];

cell.detailTextLabel.text = [dataArray[indexPath.row][@"age"] stringValue];

return cell;

}

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

return 59;

}

@end

复制代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容