#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *_tableView;
// tableView 的数据源
NSMutableArray *dataArray;
// 存放从plist文件中读取的内容
NSMutableDictionary *dict;
// 用来给tableView的编辑器设置不同的状态
BOOL isInsert;
}
@end
@implementation ViewController
/*
关于UITableView的调错方法
1、通过打断点检查代理方法是否执行,如果代理方法执行,认真检查代理方法里的代码
2、如果代理方法不执行,检查是否遵守协议和挂代理
3、检查数据源是否有数据以及数据格式是否正确
*/
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 414, 480) style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
// 找到plist文件的路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"Property List" ofType:@"plist"];
// 通过路径找到字典
dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@=",dict);
// 取出dict中的dataArray数组,并转变为可变数组
dataArray = [dict[@"DataArray"]mutableCopy];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 736-50, 414, 50)];
toolBar.backgroundColor = [UIColor greenColor];
UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trash)];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];
UIBarButtonItem *add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
toolBar.items = @[trash,refresh,add];
[self.view addSubview:toolBar];
}
/*
1、设置编辑模式
tableView setEditing 如果设为yes,表明tableView处于编辑模式,否则为非编辑模式
2、编辑模式有两种状态
①删除状态
②插入状态
*/
-(void)trash{
isInsert = NO;
// 获取到此时tableView的编辑模式
BOOL kbool = !_tableView.isEditing;
// 通过setEditing给tableView
[_tableView setEditing:kbool animated:YES];
}
//tableView进入编辑模式,会调用这个方法来确定编辑模式的状态,如果不实现这个方法,默认就是删除状态
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (isInsert ==YES) {
return UITableViewCellEditingStyleInsert;
}else{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;
}
//确定点击的是何种状态会调用这个方法,实现这个方法会让cell自带一个右滑效果,删除一个cell会重新执行numberOfRowsInSection方法,从而让要删除的cell消失,不会重新执行cellForRowAtIndexPath方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}else if (editingStyle == UITableViewCellEditingStyleInsert){
NSMutableDictionary *tempDict = [NSMutableDictionary dictionary ];
// 包装一条数据
tempDict[@"name"] = @"小薇";
tempDict[@"content"] = @"花好香";
tempDict[@"icon"] = @"xiaowei";
// 将包装好的数据放到数据源数组
[dataArray insertObject:tempDict atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
-(void)refresh{
dataArray = [dict[@"DataArray"]mutableCopy];
// reloadData让tableView重新加载
[_tableView reloadData];
}
-(void)add{
isInsert = YES;
// 让tableView在编辑和非编辑状态切换
// 获取到此时tableView的编辑模式
BOOL kbool = !_tableView.isEditing;
// 通过setEditing给tableView
[_tableView setEditing:kbool animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cellId";
// 在tableView里查找是不是有@"cellId"这个标记的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.textLabel.text = dataArray[indexPath.row][@"name"];
cell.detailTextLabel.text = dataArray[indexPath.row][@"content"];
cell.backgroundColor = [UIColor greenColor];
return cell;
}
@end