課程要點(diǎn):plist文件的新建與讀取給UITableView設(shè)置變化的值單元格的刪除、插入及刷新? ? ? plist文件的新建與讀取?
?新建plist? Commadn+N,iOS->Resouce->Property List ? ? ? ? ?

? ?plist文件還有另外一種展現(xiàn)形式? 右鍵plist文件,open as->Property List就是上面顯示的這種方式來(lái)展現(xiàn), open as->source Code 就是下面這種方式來(lái)展現(xiàn)

? ? ? ?這種形式下我可以通過(guò)改變代碼或者增加代碼給plist修改或增加數(shù)據(jù)。? 讀取plist數(shù)據(jù)? 為了咱們的數(shù)據(jù)統(tǒng)一,我把我的plist里的代碼留下。

? ? ? 復(fù)制代碼DataArrayage16name王五name張三age18name李四age19age16name趙六復(fù)制代碼? ? 讀取plist文件分兩步? ? 1、找到plist文件在boundle中的路徑,? ? 2、使用字典讀取該文件? ? ? ? boundles:iOS的應(yīng)用都是通過(guò)bundle進(jìn)行封裝的,對(duì)應(yīng)的bundle類型是Application類型,平時(shí)我們通過(guò)XCode編譯出來(lái)的Target(即我們開(kāi)發(fā)的應(yīng)用),其實(shí)就是一個(gè)Application類型bundle,即一個(gè)文件夾!

? ?運(yùn)行后控制臺(tái)輸出:


? 給UITableView設(shè)置變化的值



?源代碼出處: 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的數(shù)據(jù)源? ? NSMutableArray *dataArray;? ? ? ? UITableView *_tableView;}@end@implementation ViewController- (void)viewDidLoad {? ? [super viewDidLoad];? ? ? ? //獲取到plist文件路徑? ? NSString *path = [[NSBundle mainBundle] pathForResource:@"DataPlist" ofType:@"plist"];? ? //通過(guò)字典獲取到plist文件中的字典? ? NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];? ? ? ? NSLog(@"數(shù)據(jù)=%@",dict);? ? ? ? //將plist文件中的數(shù)組賦值給工程中的數(shù)據(jù)源數(shù)組? ? 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設(shè)置行數(shù)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{? ? ? ? //數(shù)據(jù)源數(shù)組里面有幾個(gè)元素,里面就有幾行? ? return dataArray.count;}//每次將要顯示cell,就會(huì)調(diào)用這個(gè)方法,在這個(gè)方法內(nèi)設(shè)置一個(gè)cell并返回就會(huì)就將cell放到tableView上面,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{? ? ? ? /*? ? * 這個(gè)方法會(huì)傳進(jìn)來(lái)兩個(gè)參數(shù)? ? ? ? ? * tableView:就是咱們正在操作的tableView? ? ? ? * indexPath:這個(gè)值有兩個(gè)屬性section和row,分別標(biāo)明這個(gè)cell位于第幾段第幾行? ? ? ? ? */? ? ? ? static NSString *cellID = @"cellID";? ? ? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];? ? ? ? if (cell == nil) {? ? ? ? ? ? ? ? cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];? ? }? ? ? ? cell.backgroundColor = [UIColor yellowColor];? ? ? ? ? ? //利用傳進(jìn)來(lái)的indexpath參數(shù),來(lái)分別取數(shù)組里相應(yīng)的字典,在通過(guò)key值得到咱們真正需要的數(shù)據(jù)? ? cell.textLabel.text = dataArray[indexPath.row][@"name"];? ? cell.detailTextLabel.text = [dataArray[indexPath.row][@"age"] stringValue];? ? ? ? return cell;? ? }@end復(fù)制代碼單元格的刪除、插入及刷新UITableView有兩種模式1、正常模式2、編輯模式? 1)插入狀態(tài)? 2)刪除狀態(tài)左邊是正常模式,中間是編輯模式下的刪除狀態(tài),只要點(diǎn)擊紅色的減號(hào),單元格右邊就會(huì)出現(xiàn)刪除按鈕,如第三張圖片所示。? ? ? ? 1、獲取到tableView編譯模式,tableView是UITableView的一個(gè)對(duì)象tableView.iSEditing 2、給tableVIew設(shè)置編輯模式[tableView setEditing:YES animated:YES];3、只要進(jìn)入編輯模式,系統(tǒng)就會(huì)自動(dòng)調(diào)用這個(gè)方法來(lái)詢問(wèn)是要進(jìn)入編輯模式下的刪除狀態(tài)還是插入狀態(tài)。如果不實(shí)現(xiàn)這個(gè)方法,系統(tǒng)默認(rèn)是刪除狀態(tài)。復(fù)制代碼- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{? ? ? ? //這兩種你可以都試試,看一下效果,但最終要選擇刪除模式,? ? return UITableViewCellEditingStyleDelete;//? ? return UITableViewCellEditingStyleInsert;? ? }復(fù)制代碼? 4、進(jìn)入編輯模式后,通過(guò)上面的代理方法進(jìn)入相應(yīng)的狀態(tài),此時(shí)不管你點(diǎn)擊的是刪除按鈕還是插入按鈕,都會(huì)進(jìn)入下面這個(gè)方法。實(shí)現(xiàn)這個(gè)方法直接右劃也可以出現(xiàn)右邊的刪除按鈕復(fù)制代碼- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{? ? ? ? /*? ? ? ? 如果點(diǎn)擊的是刪除按鈕,editingStyle參數(shù)就是UITableViewCellEditingStyleDelete? ? ? ? ? ? 如果點(diǎn)擊的是插入按鈕,editingStyle參數(shù)就是UITableViewCellEditingStyleInsert? ? ? ? ? ? indexPath:能夠獲得你點(diǎn)擊是哪一行哪一段。? ? ? ? ? ? ? */? ? ? ? //通過(guò)判斷你點(diǎn)擊的是刪除還是插入做不同的操作? ? if (editingStyle == UITableViewCellEditingStyleDelete) {? ? ? ? //刪除dataArray中相應(yīng)cell的數(shù)據(jù)? ? ? ? [dataArray removeObjectAtIndex:indexPath.row];? ? ? ? //刪除cell? ? ? ? [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];? ? }else if (editingStyle == UITableViewCellEditingStyleInsert){? ? ? ? NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];? ? ? ? tempDict[@"name"] = @"小閃";? ? ? ? tempDict[@"age"] = @(18);? ? ? ? //在數(shù)據(jù)源數(shù)組中增加一個(gè)字典? ? ? ? ? [dataArray insertObject:tempDict atIndex:indexPath.row];? ? ? ? //插入一個(gè)cell? ? ? ? ? [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];? ? }? ? ? ? }復(fù)制代碼完整代碼如下:復(fù)制代碼////? ViewController.m//? Plist文件以及cell的增加與刪除////? Created by 王立廣 on 15/12/14.//? Copyright ? 2015年 王立廣. All rights reserved.//#import "ViewController.h"@interface ViewController (){? ? //UITableView的數(shù)據(jù)源? ? NSMutableArray *dataArray;? ? ? ? UITableView *_tableView;}@end@implementation ViewController- (void)viewDidLoad {? ? [super viewDidLoad];? ? ? ? //獲取到plist文件路徑? ? NSString *path = [[NSBundle mainBundle] pathForResource:@"DataPlist" ofType:@"plist"];? ? ? ? //通過(guò)字典獲取到plist文件中的字典? ? NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];? ? ? ? NSLog(@"數(shù)據(jù)=%@",dict[@"DataArray"][1][@"name"]);? ? ? ? //將plist文件中的數(shù)組賦值給工程中的數(shù)據(jù)源數(shù)組? ? 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上面加一個(gè)刪除按鈕? ? toolBar.items = @[trash];? ? }- (void)trash{? ? ? ? //tableView.iSEditing獲得tableView是否屬于編輯模式,通過(guò)取反來(lái)改變tableView的編輯模式? ? BOOL kbool = !_tableView.isEditing;? ? ? ? // tableView setEditing給tableView設(shè)置編輯模式? ? [_tableView setEditing:kbool animated:YES];}//只要進(jìn)入編輯模式,系統(tǒng)就會(huì)自動(dòng)調(diào)用這個(gè)方法來(lái)詢問(wèn)是要進(jìn)入編輯模式下的刪除狀態(tài)還是插入狀態(tài)。如果不實(shí)現(xiàn)這個(gè)方法,系統(tǒng)默認(rèn)是刪除狀態(tài)。- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{? ? ? ? //這兩種你可以都試試,看一下效果? ? return UITableViewCellEditingStyleDelete;//? ? return UITableViewCellEditingStyleInsert;? ? }//進(jìn)入編輯模式后,通過(guò)上面的代理方法進(jìn)入相應(yīng)的狀態(tài),此時(shí)不管你點(diǎn)擊的是刪除按鈕還是插入按鈕,都會(huì)進(jìn)入下面這個(gè)方法。實(shí)現(xiàn)這個(gè)方法直接右劃也可以出現(xiàn)右邊的刪除按鈕- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{? ? ? ? /*? ? ? ? 如果點(diǎn)擊的是刪除按鈕,editingStyle參數(shù)就是UITableViewCellEditingStyleDelete? ? ? ? ? ? 如果點(diǎn)擊的是插入按鈕,editingStyle參數(shù)就是UITableViewCellEditingStyleInsert? ? ? ? ? ? indexPath:能夠獲得你點(diǎn)擊是哪一行哪一段。? ? ? ? ? ? ? */? ? ? ? //通過(guò)判斷你點(diǎn)擊的是刪除還是插入做不同的操作? ? if (editingStyle == UITableViewCellEditingStyleDelete) {? ? ? ? //刪除dataArray中相應(yīng)cell的數(shù)據(jù)? ? ? ? [dataArray removeObjectAtIndex:indexPath.row];? ? ? ? ? ? ? ? ? //刪除cell時(shí),沒(méi)有執(zhí)行cellForRowAtIndexPath方法? ? ? ? [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];? ? }else if (editingStyle == UITableViewCellEditingStyleInsert){? ? ? ? ? ? ? ? NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];? ? ? ? tempDict[@"name"] = @"小閃";? ? ? ? tempDict[@"age"] = @(18);? ? ? //在數(shù)據(jù)源數(shù)組中增加一個(gè)字典? ? ? [dataArray insertObject:tempDict atIndex:indexPath.row];? ? ? ? ? //插入一個(gè)cell? ? ? [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];? }? ? ? ? }//給tableView設(shè)置行數(shù)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{? ? //數(shù)據(jù)源數(shù)組里面有幾個(gè)元素,里面就有幾行? ? return dataArray.count;}//每次將要顯示cell,就會(huì)調(diào)用這個(gè)方法,在這個(gè)方法內(nèi)設(shè)置一個(gè)cell并返回就會(huì)就將cell放到tableView上面,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{? ? ? ? //用static修飾的變量只會(huì)初始化一次。? ? static NSString *cellID = @"cellID";? ? ? ? /*? ? * 這個(gè)方法會(huì)傳進(jìn)來(lái)兩個(gè)參數(shù)? ? ? ? ? * tableView:就是咱們正在操作的tableView? ? ? ? ? * indexPath:這個(gè)值有兩個(gè)屬性section和row,分別標(biāo)明這個(gè)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復(fù)制代碼PS:咱們?cè)趧h除cell的同時(shí)也在數(shù)組中刪除了cell所對(duì)應(yīng)的數(shù)據(jù)。并沒(méi)有刪除plist文件中的內(nèi)容。 單元格的刷新 改動(dòng)有三: 1、將存放plist數(shù)據(jù)的字典設(shè)置為全局變量 2、在toolBar上新增了一個(gè)refresh按鈕 3、在refresh按鈕的觸發(fā)方法里,重新給數(shù)據(jù)源數(shù)組賦值,然后使用UITableView的reloadData方法刷新表格。? PS:reloadData刷新的意思是tableView的所有代理方法重走。重新計(jì)算多少行,重新設(shè)置cell等等。。。。復(fù)制代碼////? ViewController.m//? Plist文件以及cell的增加與刪除////? Created by 王立廣 on 15/12/14.//? Copyright ? 2015年 王立廣. All rights reserved.//#import "ViewController.h"@interface ViewController (){





//UITableView的數(shù)據(jù)源
NSMutableArray *dataArray;
UITableView *_tableView;
//從plist文件中取出來(lái)的字典
NSDictionary *dict;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//獲取到plist文件路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"DataPlist" ofType:@"plist"];
//通過(guò)字典獲取到plist文件中的字典
dict = [[NSDictionary alloc]initWithContentsOfFile:path];
NSLog(@"數(shù)據(jù)=%@",dict[@"DataArray"][1][@"name"]);
//將plist文件中的數(shù)組賦值給工程中的數(shù)據(jù)源數(shù)組
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上面加一個(gè)刪除按鈕
toolBar.items = @[trash,refresh];
}
- (void)refresh{
dataArray = [dict[@"DataArray"] mutableCopy];
[_tableView reloadData];
}
- (void)trash{
//tableView.iSEditing獲得tableView是否屬于編輯模式,通過(guò)取反來(lái)改變tableView的編輯模式
BOOL kbool = !_tableView.isEditing;
// tableView setEditing給tableView設(shè)置編輯模式
[_tableView setEditing:kbool animated:YES];
}
//只要進(jìn)入編輯模式,系統(tǒng)就會(huì)自動(dòng)調(diào)用這個(gè)方法來(lái)詢問(wèn)是要進(jìn)入編輯模式下的刪除狀態(tài)還是插入狀態(tài)。如果不實(shí)現(xiàn)這個(gè)方法,系統(tǒng)默認(rèn)是刪除狀態(tài)。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//這兩種你可以都試試,看一下效果,
return UITableViewCellEditingStyleDelete;
//? ? return UITableViewCellEditingStyleInsert;
}
//進(jìn)入編輯模式后,通過(guò)上面的代理方法進(jìn)入相應(yīng)的狀態(tài),此時(shí)不管你點(diǎn)擊的是刪除按鈕還是插入按鈕,都會(huì)進(jìn)入下面這個(gè)方法。實(shí)現(xiàn)這個(gè)方法直接右劃也可以出現(xiàn)右邊的刪除按鈕
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
/*
如果點(diǎn)擊的是刪除按鈕,editingStyle參數(shù)就是UITableViewCellEditingStyleDelete
如果點(diǎn)擊的是插入按鈕,editingStyle參數(shù)就是UITableViewCellEditingStyleInsert
indexPath:能夠獲得你點(diǎn)擊是哪一行哪一段。
*/
//通過(guò)判斷你點(diǎn)擊的是刪除還是插入做不同的操作
if (editingStyle == UITableViewCellEditingStyleDelete) {
//刪除dataArray中相應(yīng)cell的數(shù)據(jù)
[dataArray removeObjectAtIndex:indexPath.row];
//刪除cell時(shí),沒(méi)有執(zhí)行cellForRowAtIndexPath方法
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationTop];
}else if (editingStyle == UITableViewCellEditingStyleInsert){
NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
tempDict[@"name"] = @"小閃";
tempDict[@"age"] = @(18);
//在數(shù)據(jù)源數(shù)組中增加一個(gè)字典
[dataArray insertObject:tempDict atIndex:indexPath.row];
//插入一個(gè)cell
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
}
//給tableView設(shè)置行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//數(shù)據(jù)源數(shù)組里面有幾個(gè)元素,里面就有幾行
return dataArray.count;
}
//每次將要顯示cell,就會(huì)調(diào)用這個(gè)方法,在這個(gè)方法內(nèi)設(shè)置一個(gè)cell并返回就會(huì)就將cell放到tableView上面,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//用static修飾的變量只會(huì)初始化一次。
static NSString *cellID = @"cellID";
/*
* 這個(gè)方法會(huì)傳進(jìn)來(lái)兩個(gè)參數(shù)
* tableView:就是咱們正在操作的tableView
* indexPath:這個(gè)值有兩個(gè)屬性section和row,分別標(biāo)明這個(gè)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
復(fù)制代碼