做一个tableView,包含增删改移动功能,并且修改值的时候,在按home键的时候会自动保存。如果可以的话使者保存自定义的类数组保存到plist中。

实现步骤:

1.创建一个SingleViewApplication的项目,首页命名为FirstViewController


FirstViewController.h:

#import <UIKit/UIKit.h>

@interface FirstViewController : UITableViewController
@property(nonatomic,retain) NSMutableArray *array;
@end

FirstViewController.m:

#import "FirstViewController.h"
#import "DXWViewController.h"
@interface FirstViewController () @end
NSString *CellIdentifier = @"Cell"; @implementation FirstViewController - (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.array = @[
[[DXWViewController alloc] initWithStyle:UITableViewStylePlain]];
self.title = @"目录列表";
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ return [self.array count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; int row = [indexPath row];
cell.textLabel.text = ((DXWViewController *)self.array[row]).title;
//有右边的>符号
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = [indexPath row];
[self.navigationController pushViewController:self.array[row] animated:YES];
} @end

2.创建二级子视图控制器,不带xib文件,命名DXWViewController


DXWViewController.h:

#import <UIKit/UIKit.h>

@protocol change <NSObject>

-(void)changeData:(int)row Name:(NSString *)name;

@end

@interface DXWViewController : UITableViewController<UIAlertViewDelegate>
@property(nonatomic,retain)NSMutableArray *array;
@property(retain,nonatomic)NSIndexPath *indexPath;
@end

DXWViewController.m:

#import "DXWViewController.h"
#import "EditViewController.h"
BOOL RIGHTBUTTON;
@interface DXWViewController () @end @implementation DXWViewController //获取沙盒文件路径 -(NSString *)getPath { //用来获得Document地址 NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//注意:这里是NSDocument不是NSDocumentation,特别注意 NSLog(@"%@",arr); //在地址上增加文件 NSString *path = [arr[0] stringByAppendingPathComponent:@"abc.plist"]; NSLog(@"%@",path); return path; } -(BOOL)writeDataToFile:(NSString *)path DataSource:(NSMutableArray *)array
{
return [array writeToFile:path atomically:YES];
} -(NSMutableArray *)getDataFromFile:(NSString *)path
{
//判断是否有文件 if([[NSFileManager defaultManager] fileExistsAtPath:[self getPath]]) { NSMutableArray *arr = [NSArray arrayWithContentsOfFile:[self getPath]]; return arr; }
} - (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self)
{
self.title = @"子视图";
// NSArray *arr = @[@"返回上一级",@"小花",@"小李",@"小王",@"小丁",@"小张",@"小康",@"小刘",@"小墩",@"大墩"];
// self.array = [arr mutableCopy];
// if ([self writeDataToFile:[self getPath] DataSource:self.array]) {
// NSLog(@"success");
// }
//获取数据
self.array = [self getDataFromFile:[self getPath]];
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
//首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleBordered target:self action:@selector(itemLeftButtonClick:)];
self.navigationItem.leftBarButtonItem = button; UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(itemRightButtonClick:)];
self.navigationItem.rightBarButtonItem = button1;
} - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.array count];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
int row = [indexPath row];
cell.textLabel.text = self.array[row];
return cell;
} -(void)itemLeftButtonClick:(id)sender
{ //设置可以编辑
RIGHTBUTTON = false;
[self.tableView setEditing:!self.tableView.editing];//设置成和当前状态相反的状态
if (self.tableView.editing)
{
[self.navigationItem.leftBarButtonItem setTitle:@"完成"];
}
else
{
[self.navigationItem.leftBarButtonItem setTitle:@"添加"];
}
} -(void)itemRightButtonClick:(id)sender
{
RIGHTBUTTON = true;
//设置可以编辑
[self.tableView setEditing:!self.tableView.editing];//设置成和当前状态相反的状态
if (self.tableView.editing)
{
[self.navigationItem.rightBarButtonItem setTitle:@"完成"];
}
else
{
[self.navigationItem.rightBarButtonItem setTitle:@"删除"];
}
} //设置编译图标
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//删除
if (RIGHTBUTTON)
{
return UITableViewCellEditingStyleDelete;
}
else
{
return UITableViewCellEditingStyleInsert;
//return UITableViewCellEditingStyleNone;
}
}
//设置是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (RIGHTBUTTON)
{
return NO;
}
else
{
if (indexPath != 0) {
return YES;
}
}
} -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] == 0) {
return NO;
}
else
{
return YES;
}
} //实现UIAlertView代理协议
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
//获取AlertView中的数据
NSString *str = [alertView textFieldAtIndex:0].text;
//将数据插入到array数组中(位置是点击的位置的下一行)
[self.array insertObject:str atIndex:[self.indexPath row]+1];
//用点击位置的下一行创建一个新的NSIndexPath
int newrow = [self.indexPath row]+1;
NSIndexPath *index = [NSIndexPath indexPathForRow:newrow inSection:0];
//在tableView的这个NSIndexPath中插入数据
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation: UITableViewRowAnimationFade];
[self writeDataToFile:[self getPath] DataSource:self.array];
} -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//删除按钮
if (RIGHTBUTTON) {
if ([indexPath row] != 0)
{
int row = [indexPath row];
[self.array removeObjectAtIndex:row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[self writeDataToFile:[self getPath] DataSource:self.array];
}
}
//添加按钮
else
{
//获得点击的indexPath
self.indexPath = indexPath;
//创建一个UIAlertView,用来获得数据
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"enterstring" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
//带有输入框的UIAlertView
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
} //可以根据行来设置delete按钮位置上button的标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"你删我一个试试";
} -(void)dealloc
{
[self.array release];
[self.indexPath release];
[super dealloc];
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath row] == 0)
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
else
{
EditViewController *edit = [[EditViewController alloc] init];
edit.sname = self.array[[indexPath row]];
edit.row = indexPath.row;
edit.delegate = self;
[self.navigationController pushViewController:edit animated:YES];
}
//蓝色背景消失
[tableView deselectRowAtIndexPath:indexPath animated:YES];
} -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
if (!RIGHTBUTTON) {
if ([destinationIndexPath row]!=0) {
id object= self.array[[sourceIndexPath row]];
[self.array removeObject:self.array[[sourceIndexPath row]]];
[self.array insertObject:object atIndex:[destinationIndexPath row]];
NSLog(@"%d",[self.array count]);
NSLog(@"当前位置%d,目标位置%d",[sourceIndexPath row],[destinationIndexPath row]);
[self writeDataToFile:[self getPath] DataSource:self.array];
}
//如果是跟第一行移动,则不允许这样操作,重新加载数据
[self.tableView reloadData];
}
} //实现遵循协议的方法
-(void)changeData:(int)row Name:(NSString *)name
{
[self.array replaceObjectAtIndex:row withObject:name];
[self writeDataToFile:[self getPath] DataSource:self.array];
[self.tableView reloadData];
}
@end

3.创建一个可以修改的viewController With xib,命名为:EditViewController

EditViewController.h:

#import <UIKit/UIKit.h>
#import "DXWViewController.h"
@interface EditViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *name;
@property(retain,nonatomic)NSString *sname;
@property(assign,nonatomic) int row;
- (IBAction)assignBoardClick:(id)sender;
@property(nonatomic,retain)id<change>delegate;
@end

EditViewController.m:

#import "EditViewController.h"
#import "DXWViewController.h"
@interface EditViewController () @end @implementation EditViewController
//获取沙盒文件路径 -(NSString *)getPath { //用来获得Document地址 NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//注意:这里是NSDocument不是NSDocumentation,特别注意 NSLog(@"%@",arr); //在地址上增加文件 NSString *path = [arr[0] stringByAppendingPathComponent:@"abc.plist"]; NSLog(@"%@",path); return path; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"信息修改";
}
return self;
} -(void)viewWillAppear:(BOOL)animated
{
self.name.text = self.sname;
} - (void)viewDidLoad
{
[super viewDidLoad];
//首先要添加右上角的一个edit按钮,按钮按下去可以设置可以编辑
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(itemLeftButtonClick:)];
//左按钮返回
self.navigationItem.leftBarButtonItem = button;
//右按钮编辑保存并返回
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStyleBordered target:self action:@selector(itemRightButtonClick:)];
self.navigationItem.rightBarButtonItem = button1; //实现不按保存按钮也能够保存到文件的功能,获取应用
UIApplication *app = [UIApplication sharedApplication];
//在通知中心添加一个观察者,当符合UIApplicationWillResignActiveNotification条件时,调用方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(save:) name:UIApplicationWillResignActiveNotification object:app];
} -(void)save:(id)sender
{
self.sname = self.name.text;
if ([self.delegate respondsToSelector:@selector(changeData:Name:)]) {
[self.delegate changeData:self.row Name:self.sname];
}
} //返回
-(void)itemLeftButtonClick:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
//保存
-(void)itemRightButtonClick:(id)sender
{
[self changedata];
[self.navigationController popViewControllerAnimated:YES];
} - (void)changedata
{
self.sname = self.name.text;
if ([self.delegate respondsToSelector:@selector(changeData:Name:)]) {
[self.delegate changeData:self.row Name:self.sname];
}
} - (IBAction)assignBoardClick:(id)sender
{
[sender resignFirstResponder];
} - (void)dealloc {
[_name release];
[super dealloc];
}
@end


demo源文件:

http://download.csdn.net/detail/s10141303/6008305


[IOS]包含增删改查移动的tableView展示+plist文件保存+程序意外退出保存Demo的更多相关文章

  1. iOS CoreData 增删改查详解

    最近在学习CoreData, 因为项目开发中需要,特意学习和整理了一下,整理出来方便以后使用和同行借鉴.目前开发使用的Swift语言开发的项目.所以整理出来的是Swift版本,OC我就放弃了. 虽然S ...

  2. 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包含增删改查、JavaBean反射原理,附源码)

    最近看老罗的视频,跟着完成了利用Java操作MySql数据库的一个框架类JdbcUtils.java,完成对数据库的增删改查.其中查询这块,包括普通的查询和利用反射完成的查询,主要包括以下几个函数接口 ...

  3. iOS sqlite 增删改查 简单封装(基于 FMDB)

    /** *  对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * *  基于 FMDB * *  操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整 ...

  4. 针对MySql封装的JDBC通用框架类(包含增删改查、JavaBean反射原理)

    package com.DBUtils; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.Dri ...

  5. iOS SQLite 增删改查的封装(关系型)

    在工程里导入libsqlite3.tbd库(Xcode 7) #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder &l ...

  6. iOS SQLite增删改查(简单应用)

    // 注意: 在工程里导入libsqlite3.tbd库(Xcode7,如果Xcode7以下的版本则导入libsqlite3.dylib). #import <UIKit/UIKit.h> ...

  7. 一个完整的mybatis项目,包含增删改查

    1.导入jar包,导入相关配置文件,均在自己博客园的文件中 编写mybatis.xml文件 <?xml version="1.0" encoding="UTF-8& ...

  8. IOS - CoreData 增删改查

    #pragma mark - Core Data Methods - (void)insertObjectWithFileName:(NSString *)fileName { /** SQL新增记录 ...

  9. 在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查

    一.首先先创建一个C#的控制台应用 二.然后添加上必要的命名空间 using System;using System.Collections.Generic;using MySql.Data.MySq ...

随机推荐

  1. hdu 4277 USACO ORZ(dfs+剪枝)

    Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...

  2. Shell中特殊的变量

    $表示当前的进程,当使用echo $$是会输出当前shell的pid echo $$ 特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例 ...

  3. Oracle_表数据拆分与合并

    参考文档: [1]http://blog.itpub.net/8858072/viewspace-426960/ [2]http://blog.csdn.net/mattlinsheep/articl ...

  4. python实现websocket服务器,可以在web实时显示远程服务器日志

    一.开始的话 使用python简单的实现websocket服务器,可以在浏览器上实时显示远程服务器的日志信息. 之前做了一个web版的发布系统,但没实现在线看日志,每次发布版本后,都需要登录到服务器上 ...

  5. CentOS6.7 用户

    1.添加普通用户[root@server ~]# useradd chenjiafa   //添加一个名为chenjiafa的用户[root@server ~]# passwd chenjiafa   ...

  6. 颜色矩阵 滤镜 ColorMatrix

    颜色矩阵原理 色彩的三要素 1.色相.色相通俗的说就是"颜色",色相的改变就是颜色的改变,色相的调节伴随着红橙黄绿蓝紫的变化. 2.亮度.明度通俗的说就是"光照度&quo ...

  7. 【VS2015正式版下载】Visual Studio 2015 正式版开放下载 Visual Studio 2015 神key

    说明: 微软定于2015年7月20日发布Visual Studio 2015正式版,目前其官方网站已经提供正式版本的下载. 可在https://www.visualstudio.com/en-us/d ...

  8. python 下的数据结构与算法---4:线形数据结构,栈,队列,双端队列,列表

    目录: 前言 1:栈 1.1:栈的实现 1.2:栈的应用: 1.2.1:检验数学表达式的括号匹配 1.2.2:将十进制数转化为任意进制 1.2.3:后置表达式的生成及其计算 2:队列 2.1:队列的实 ...

  9. (转)SQL NEWID()随机函数

    从A表随机取2条记录,用SELECT TOP 10 * FROM ywle order by newid()order by 一般是根据某一字段排序,newid()的返回值 是uniqueidenti ...

  10. 反射那些事儿——Java动态装载和反射技术

    一直以来反射都是只闻其声,却无法将之使用,近日尽心下来学习下,发现了很多精妙之处. Java动态装载和反射技术 一.类的动态装载 1.Java代码编译和执行的整个过程包含了以下三个重要的机制: ● J ...