標籤:
//// ViewController.m// 表格的修改//// Created by YaguangZhu on 15/8/16.// Copyright (c) 2015年 YaguangZhu. All rights reserved.//#import "ViewController.h"@interface ViewController ()<UITabBarControllerDelegate>@property (nonatomic,strong)NSMutableArray *dataList;@property (nonatomic,strong)UITableView *tableView;@end@implementation ViewController- (UITableView *)tableView{ if (_tableView == nil) { _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; } return _tableView;}- (NSMutableArray *)dataList{ if (_dataList == nil) { _dataList = [NSMutableArray arrayWithObjects:@"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwuwangwuwangwuwangwuwangwu", nil]; } return _dataList;}- (void)viewDidLoad { [super viewDidLoad]; [self tableView]; // 開始編輯,一旦editing == YES就預設開啟刪除模式 self.tableView.editing = YES; // Do any additional setup after loading the view, typically from a nib.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataList.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } // 設定表格 cell.textLabel.text = self.dataList[indexPath.row]; return cell;}// 只要實現了此方法,就能夠支援手勢拖拽刪除了,刪除需要自己幹!/** UITableViewCellEditingStyleNone, UITableViewCellEditingStyleDelete, 刪除 UITableViewCellEditingStyleInsert 添加 */- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { NSLog(@"要刪除"); // MVC => 資料是儲存在模型中 // 1. 刪除self.dataList中indexPath對應的資料 [self.dataList removeObjectAtIndex:indexPath.row]; NSLog(@"%@", self.dataList); // 2. 重新整理表格(重新載入資料) // 重新載入所有資料 // [self.tableView reloadData]; // deleteRowsAtIndexPaths讓表格控制項動畫刪除指定的行 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { NSLog(@"要添加資料"); // 1. 向數組添加資料 [self.dataList insertObject:@"王小二" atIndex:indexPath.row + 1]; // 2. 重新整理表格 // [self.tableView reloadData]; // insertRowsAtIndexPaths讓表格控制項動畫在指定indexPath添加指定行 // 建立一個indexPath NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle]; }}#pragma mark - 代理方法// 返回編輯樣式,如果沒有實現此方法,預設都是刪除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ // if (indexPath.row % 2) { // return UITableViewCellEditingStyleInsert; // } else { // return UITableViewCellEditingStyleDelete; // } return UITableViewCellEditingStyleInsert;}// 只要實現此方法,就可以顯示拖動控制項- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{ // 介面資料UITableView已經完成了 // 調整資料即可 // [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; // 1. 將源從數組中取出 id source = self.dataList[sourceIndexPath.row]; // 2. 將源從數組中刪除 [self.dataList removeObjectAtIndex:sourceIndexPath.row]; NSLog(@"%@", self.dataList); // 3. 將源插入到數組中的目標位置 [self.dataList insertObject:source atIndex:destinationIndexPath.row]; NSLog(@"%@", self.dataList);}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
iOS-表格式資料的添加 刪除