#import
@interface AppDelegate : UIResponder
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) NSMutableArray *ListArray;
-(NSString *)getFilePath;
@end
//
// AppDelegate.m
// 0818
//
// Created by administrator on 14-8-18.
// Copyright (c) 2014年 administrator. All rights reserved.
//
#import "AppDelegate.h"
#import "SecondViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSFileManager *file=[NSFileManager defaultManager];
if ([file fileExistsAtPath:[self getFilePath]]) {
self.ListArray=[NSMutableArray arrayWithContentsOfFile:[self getFilePath]];
}else{
self.ListArray=[NSMutableArray array];
}
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
SecondViewController *vc=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController=nav;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if ([self.ListArray writeToFile:[self getFilePath] atomically:YES]) {
NSLog(@"存档成功");
}else{
NSLog(@"存档失败");
}
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
-(NSString *)getFilePath
{
//获得沙盒路径Documents文件夹
NSString *homePath = NSHomeDirectory();
NSString *path = [NSString stringWithFormat:@"%@/Documents/521521.plist",homePath];
return path;
}
@end
second.h
#import
@interface SecondViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *ListTableView;
@end
second.m
#import "SecondViewController.h"
#import "AppDelegate.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "CustomCell.h"
@interface SecondViewController ()
{
AppDelegate *del;
}
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.ListTableView.dataSource=self;
self.ListTableView.delegate=self;
self.ListTableView.allowsMultipleSelectionDuringEditing=YES;
del=[[UIApplication sharedApplication]delegate];
self.title=@"备忘录";
UIBarButtonItem *adds=[[UIBarButtonItem alloc]initWithTitle:@"ADD" style:UIBarButtonItemStylePlain target:self action:@selector(add:)];
[self.navigationItem setRightBarButtonItem:adds];
UIBarButtonItem *deleteItem=[[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(edit:)];
deleteItem.tag=1;
[self.navigationItem setLeftBarButtonItem:deleteItem];
//设置导航栏颜色
// [self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
// self.navigationController.navigationBar.translucent=YES;
UIBarButtonItem *back=[[UIBarButtonItem alloc]init];
back.title=@"返回";
self.navigationItem.backBarButtonItem =back;
}
-(void)edit:(UIBarButtonItem *)sender
{
if (sender.tag==1) {
self.ListTableView.editing=YES;
[sender setTitle:@"Done"];
sender.tag=2;
}else{
[self deleteSelectedRows];
self.ListTableView.editing=NO;
[sender setTitle:@"Edit"];
sender.tag=1;
}
}
- (void)deleteSelectedRows
{
NSArray *selectedRows = [self.ListTableView indexPathsForSelectedRows];
if ([selectedRows count] == 0) {
return;
}
NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
for (NSIndexPath *selectionIndex in selectedRows)
{
[indicesOfItemsToDelete addIndex:selectionIndex.row];
}
// Delete the objects from our data model.
[del.ListArray removeObjectsAtIndexes:indicesOfItemsToDelete];
// Tell the tableView that we deleted the objects
[self.ListTableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
// [self.todoListTable reloadData];
}
-(void)viewWillAppear:(BOOL)animated
{
//刷新数据,重新加载UITableView
[self.ListTableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [del.ListArray count];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
//自定义单元格注册,只注册一次
static BOOL nibsRegistered = NO;
if (!nibsRegistered) {
UINib *nib = [UINib nibWithNibName:@"View" bundle:nil];
[tableView registerNib:nib forCellReuseIdentifier:CustomCellIdentifier];
nibsRegistered = YES;
}
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
}
cell.showLabel.text = [del.ListArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"China.png"];
return cell;
}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 217;
}
- (void)add:(id)sender {
ThreeViewController *add = [[ThreeViewController alloc]initWithNibName:@"ThreeViewController" bundle:nil];
[self.navigationController pushViewController:add animated:YES];
// [self presentViewController:add animated:YES completion:nil];
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark - 处理删除动作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//先把数据源中的对象删掉
[del.ListArray removeObjectAtIndex:[indexPath row]];
//再删除tableView的那个行UITableViewCell
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
#pragma mark - 修改删除按钮标题
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete;
}
@end
three.h
#import
@interface ThreeViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *showText;
- (IBAction)sumbit:(id)sender;
- (IBAction)closeup:(id)sender;
@end
three.m
#import "ThreeViewController.h"
#import "AppDelegate.h"
@interface ThreeViewController ()
@end
@implementation ThreeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"添加";
self.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sumbit:(id)sender {
AppDelegate *del = [[UIApplication sharedApplication]delegate];
[del.ListArray addObject:self.showText.text];
NSString *info=[NSString stringWithFormat:@"恭喜您添加成功"];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:info delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
- (IBAction)closeup:(id)sender {
[self.showText resignFirstResponder];
}
@end
four.h
#import
@interface FourViewController : UIViewController
@property(copy,nonatomic)NSString *title1;
@property (strong, nonatomic) IBOutlet UILabel *infoLabel;
@end
four.m
#import "FourViewController.h"
@interface FourViewController ()
@end
@implementation FourViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
custom.h
@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UILabel *showLabel;
@end
custom.m
#import "CustomCell.h"
@implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end