#import "ViewController.h"
#import "MyTableViewCell.h"
#import "AddData_ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *list;
UITableView *myTabelView;
NSString *path;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add)];
[self creatTableView];
}
- (void)edit
{
[myTabelView setEditing:!myTabelView.isEditing animated:YES];
}
- (void)add
{
AddData_ViewController *addVc = [[AddData_ViewController alloc]init];
[self.navigationController pushViewController:addVc animated:YES];
}
//编辑tableView的cell
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//盘点编辑中的样式
是否是删除的样式
if (editingStyle == UITableViewCellEditingStyleDelete) {
//1.删除数据
//2.更新视图
[list removeObjectAtIndex:indexPath.row];
//把移除后的数据同步到plist里面
BOOL success = [list writeToFile:path atomically:YES];
//如果数据移除成功就删除cell
if (success) {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
}
//设置是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 如果移动的不是同一个位置
if (sourceIndexPath != destinationIndexPath) {
// 1.先保存需要移动的数据;sourceIndexPath.row需要移动的数据
NSDictionary *info = list[sourceIndexPath.row];
// 2.移除需要移动的数据
[list removeObjectAtIndex:sourceIndexPath.row];
// 3.插入数据 destinationIndexPath
可以得到需要移动的位置
[list insertObject:info atIndex:destinationIndexPath.row];
// 同步数据到plist文件
[list writeToFile:path atomically:YES];
}
}
- (void)loadData
{
path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Detail_Data_ List.plist"];
list = [[NSArray arrayWithContentsOfFile:path] mutableCopy];
NSLog( @"%@",list);
if (myTabelView) {
// reloadData必须在主线程
刷新UI
[myTabelView reloadData];
}
};
- (void)viewWillAppear:(BOOL)animated
{
[self loadData];
}
- (void)creatTableView
{
myTabelView = [[UITableView alloc]initWithFrame: self.view.frame style:UITableViewStylePlain];
myTabelView.delegate = self;
myTabelView.dataSource = self;
[self.view addSubview:myTabelView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *vigetableIndentifire = @"cellID";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vigetableIndentifire];
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:vigetableIndentifire];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.showImage.image = [UIImage imageNamed:list[indexPath.row][@"ImageName"]];
cell.nameLable.text = [NSString stringWithFormat:@"名字:%@ ",list[indexPath.row][@"name"]];
cell.priceLable .text = [NSString stringWithFormat:@"价格: %@元/斤",list[indexPath.row][@"price"]];
cell.contentLable.text = [NSString stringWithFormat:@"介绍:%@",list[indexPath.row][@"content"]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
版权声明:本文为博主原创文章,未经博主允许不得转载。