118删除单元格(扩展知识:添加单元格和移动单元格的位置)

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UITableViewController
4 @property (strong, nonatomic) NSMutableArray *mArrDataSource;
5 
6 @end

ViewController.m

  1 #import "ViewController.h"
  2 
  3 @interface ViewController ()
  4 - (void)layoutUI;
  5 @end
  6 
  7 @implementation ViewController
  8 
  9 - (void)viewDidLoad {
 10     [super viewDidLoad];
 11     
 12     [self layoutUI];
 13 }
 14 
 15 - (void)didReceiveMemoryWarning {
 16     [super didReceiveMemoryWarning];
 17     // Dispose of any resources that can be recreated.
 18 }
 19 
 20 - (void)viewDidAppear:(BOOL)animated {
 21     [super viewDidAppear:animated];
 22     
 23     //设置表格是否处于编辑模式;默认为NO
 24     [self.tableView setEditing:YES animated:YES];
 25 }
 26 
 27 - (void)layoutUI {
 28     _mArrDataSource = [[NSMutableArray alloc] initWithArray:
 29                        @[@"A", @"B", @"C", @"D", @"E", @"F", @"G",
 30                          @"H", @"I", @"J", @"K", @"L", @"M", @"N",
 31                          @"O", @"P", @"Q", @"R", @"S", @"T", @"U",
 32                          @"V", @"W", @"X", @"Y", @"Z", @"添加单元格"]];
 33     
 34     self.navigationItem.prompt = @"TableView里面,单元格也被称为Row";
 35     self.navigationItem.title = @"单元格操作(删除、添加、移动)";
 36 }
 37 
 38 #pragma mark - TableView
 39 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
 40     return @"数据列表";
 41 }
 42 
 43 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 44     return 1;
 45 }
 46 
 47 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 48     return [_mArrDataSource count];
 49 }
 50 
 51 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 52     static NSString *cellIdentifier = @"cellIdentifier";
 53     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 54     if (!cell) {
 55         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 56     }
 57     cell.textLabel.text = _mArrDataSource[indexPath.row];
 58     return cell;
 59 }
 60 
 61 #pragma mark - TableView, insert or delete row
 62 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
 63     //编辑模式下,设置最后的Row为插入模式
 64     UITableViewCellEditingStyle style = tableView.editing && indexPath.row == (_mArrDataSource.count - 1) ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
 65     return style;
 66 }
 67 
 68 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 69     switch (editingStyle) {
 70         case UITableViewCellEditingStyleNone: {
 71             break;
 72         }
 73         case UITableViewCellEditingStyleDelete: {
 74             //删除数据源数据
 75             [_mArrDataSource removeObjectAtIndex:indexPath.row];
 76             //删除单元格;注意必须保证操作之前已删除对应的数据源数据,否则报错
 77             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
 78                              withRowAnimation:UITableViewRowAnimationAutomatic];
 79             break;
 80         }
 81         case UITableViewCellEditingStyleInsert: {
 82             //添加数据源数据
 83             [_mArrDataSource insertObject:@"我是添加的单元格" atIndex:(_mArrDataSource.count - 1)];
 84             //添加单元格;注意必须保证操作之前已添加对应的数据源数据,否则报错
 85             [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
 86                              withRowAnimation:UITableViewRowAnimationAutomatic];
 87             break;
 88         }
 89     }
 90 }
 91 
 92 #pragma mark - TableView, move row
 93 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 94     //设置最后的Row不可移动
 95     return indexPath.row < (_mArrDataSource.count - 1);
 96 }
 97 
 98 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
 99     //限制单元格移动到最后的Row下方
100     NSIndexPath *indexPath = proposedDestinationIndexPath.row < (_mArrDataSource.count - 1) ? proposedDestinationIndexPath : sourceIndexPath;
101     return indexPath;
102 }
103 
104 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
105     NSUInteger sourceRow = sourceIndexPath.row;
106     NSUInteger destinationRow = destinationIndexPath.row;
107     //单元格向下移动时;循环进行当前数据与下一位数据交换位置
108     while (sourceRow < destinationRow) {
109         [_mArrDataSource exchangeObjectAtIndex:sourceRow withObjectAtIndex:(sourceRow + 1)];
110         sourceRow++;
111     }
112     //单元格向上移动时;循环进行当前数据与上一位数据交换位置
113     while (sourceRow > destinationRow) {
114         [_mArrDataSource exchangeObjectAtIndex:sourceRow withObjectAtIndex:(sourceRow - 1)];
115         sourceRow--;
116     }
117 }
118 
119 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 @property (strong, nonatomic) UIWindow *window;
5 @property (strong, nonatomic) UINavigationController *navigationController;
6 
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 
 4 @interface AppDelegate ()
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
11     ViewController *viewController = [[ViewController alloc] init];
12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
13     _window.rootViewController = _navigationController;
14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
15     [_window makeKeyAndVisible];
16     return YES;
17 }
18 
19 - (void)applicationWillResignActive:(UIApplication *)application {
20 }
21 
22 - (void)applicationDidEnterBackground:(UIApplication *)application {
23 }
24 
25 - (void)applicationWillEnterForeground:(UIApplication *)application {
26 }
27 
28 - (void)applicationDidBecomeActive:(UIApplication *)application {
29 }
30 
31 - (void)applicationWillTerminate:(UIApplication *)application {
32 }
33 
34 @end
原文地址:https://www.cnblogs.com/huangjianwu/p/4580373.html