中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[IOS]包含增刪改查移動的tableView展示+plist文件保存+程序意外退出保存Demo

發布時間:2020-07-16 13:35:09 來源:網絡 閱讀:499 作者:蓬萊仙羽 欄目:移動開發

做一個tableView,包含增刪改移動功能,并且修改值的時候,在按home鍵的時候會自動保存。如果可以的話使者保存自定義的類數組保存到plist中。

實現步驟:

1.創建一個SingleViewApplication的項目,首頁命名為FirstViewController

[IOS]包含增刪改查移動的tableView展示+plist文件保存+程序意外退出保存Demo

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

[IOS]包含增刪改查移動的tableView展示+plist文件保存+程序意外退出保存Demo[IOS]包含增刪改查移動的tableView展示+plist文件保存+程序意外退出保存Demo

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

[IOS]包含增刪改查移動的tableView展示+plist文件保存+程序意外退出保存Demo

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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

阜城县| 莫力| 织金县| 黄大仙区| 攀枝花市| 康马县| 扶沟县| 阿坝| 南溪县| 漾濞| 泽库县| 囊谦县| 达州市| 蒙阴县| 土默特左旗| 三河市| 济宁市| 广河县| 陆丰市| 克什克腾旗| 吴旗县| 萍乡市| 定安县| 鹰潭市| 佛坪县| 泊头市| 饶河县| 廉江市| 黔西县| 礼泉县| 大荔县| 星座| 沁水县| 岱山县| 同江市| 六盘水市| 金阳县| 西昌市| 富宁县| 南华县| 平昌县|