118删除单元格(扩展知识:添加单元格和移动单元格的位置)
2015-06-16 11:55
267 查看
效果如下:
[b]ViewController.h[/b]
ViewController.m
AppDelegate.h
[b]AppDelegate.m[/b]
[b]ViewController.h[/b]
#import <UIKit/UIKit.h> @interface ViewController : UITableViewController @property (strong, nonatomic) NSMutableArray *mArrDataSource; @end
ViewController.m
#import "ViewController.h" @interface ViewController () - (void)layoutUI; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self layoutUI]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //设置表格是否处于编辑模式;默认为NO [self.tableView setEditing:YES animated:YES]; } - (void)layoutUI { _mArrDataSource = [[NSMutableArray alloc] initWithArray: @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"添加单元格"]]; self.navigationItem.prompt = @"TableView里面,单元格也被称为Row"; self.navigationItem.title = @"单元格操作(删除、添加、移动)"; } #pragma mark - TableView - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"数据列表"; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_mArrDataSource count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = _mArrDataSource[indexPath.row]; return cell; } #pragma mark - TableView, insert or delete row - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { //编辑模式下,设置最后的Row为插入模式 UITableViewCellEditingStyle style = tableView.editing && indexPath.row == (_mArrDataSource.count - 1) ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete; return style; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { switch (editingStyle) { case UITableViewCellEditingStyleNone: { break; } case UITableViewCellEditingStyleDelete: { //删除数据源数据 [_mArrDataSource removeObjectAtIndex:indexPath.row]; //删除单元格;注意必须保证操作之前已删除对应的数据源数据,否则报错 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; break; } case UITableViewCellEditingStyleInsert: { //添加数据源数据 [_mArrDataSource insertObject:@"我是添加的单元格" atIndex:(_mArrDataSource.count - 1)]; //添加单元格;注意必须保证操作之前已添加对应的数据源数据,否则报错 [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; break; } } } #pragma mark - TableView, move row - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { //设置最后的Row不可移动 return indexPath.row < (_mArrDataSource.count - 1); } - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { //限制单元格移动到最后的Row下方 NSIndexPath *indexPath = proposedDestinationIndexPath.row < (_mArrDataSource.count - 1) ? proposedDestinationIndexPath : sourceIndexPath; return indexPath; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSUInteger sourceRow = sourceIndexPath.row; NSUInteger destinationRow = destinationIndexPath.row; //单元格向下移动时;循环进行当前数据与下一位数据交换位置 while (sourceRow < destinationRow) { [_mArrDataSource exchangeObjectAtIndex:sourceRow withObjectAtIndex:(sourceRow + 1)]; sourceRow++; } //单元格向上移动时;循环进行当前数据与上一位数据交换位置 while (sourceRow > destinationRow) { [_mArrDataSource exchangeObjectAtIndex:sourceRow withObjectAtIndex:(sourceRow - 1)]; sourceRow--; } } @end
AppDelegate.h
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UINavigationController *navigationController; @end
[b]AppDelegate.m[/b]
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController *viewController = [[ViewController alloc] init]; _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; _window.rootViewController = _navigationController; //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无 [_window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { } - (void)applicationDidEnterBackground:(UIApplication *)application { } - (void)applicationWillEnterForeground:(UIApplication *)application { } - (void)applicationDidBecomeActive:(UIApplication *)application { } - (void)applicationWillTerminate:(UIApplication *)application { } @end
相关文章推荐
- n个骰子的点数
- 整理AngularJS中的一些常用指令
- Express.js 中文入门指引手册
- PowerShell批量分离SMTP邮箱,并以此创建新用户邮箱
- PowerShell批量分离SMTP邮箱,并以此创建新用户邮箱
- C primer plus(第五版)第十七章链表list.h,list.c,films3.c编译警告原因
- MFC基于对话框的菜单栏,工具栏和状态栏的创建
- ruby实现网页图片抓取
- 项目中发现Element类下无getTextContent函数
- POJ 1860 Bellman-Ford算法
- 12-1路径初识
- 逆向思维 PMC云化数据中心的“技术”革命
- Android日志打印命令
- hive到hbase创建映射表以及添加数据
- IOS三方开源框架总结
- hibernate.c3p0
- FreeMarker 全文讲解
- sqlserver 中的CASE用法
- tips 前端 点击事件
- MyEclipse 2015 CI