UITableView-编辑模式(多选移动删除)
2016-07-28 17:02
495 查看
转自:http://www.cnblogs.com/gcb999/p/3349141.html
//// MJViewController.m// UITableView-编辑模式//// Created by mj on 13-4-11.// Copyright (c) 2013年 itcast. All rights reserved.//#import "MJViewController.h"@interface MJViewController () {// 当前的编辑模式UITableViewCellEditingStyle _editingStyle;}@property (nonatomic, retain) NSMutableArray *data;@end@implementation MJViewController#pragma mark - 生命周期方法- (void)viewDidLoad{[super viewDidLoad];self.data = [NSMutableArray array];for (int i = 0; i<20; i++) {NSString *text = [NSString stringWithFormat:@"mj-%i", i];[self.data addObject:text];}// 设置tableView可不可以选中//self.tableView.allowsSelection = NO;// 允许tableview多选//self.tableView.allowsMultipleSelection = YES;// 编辑模式下是否可以选中//self.tableView.allowsSelectionDuringEditing = NO;// 编辑模式下是否可以多选//self.tableView.allowsMultipleSelectionDuringEditing = YES;// 获取被选中的所有行// [self.tableView indexPathsForSelectedRows]// 获取当前可见的行// [self.tableView indexPathsForVisibleRows];}- (void)viewDidUnload {[super viewDidUnload];self.data = nil;}- (void)dealloc {[_data release];[super dealloc];}#pragma mark - 数据源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.data.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *identifier = @"UITableViewCell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];cell.detailTextLabel.text = @"详细描述";}cell.textLabel.text = [self.data objectAtIndex:indexPath.row];return cell;}#pragma mark - 代理方法#pragma mark 设置Cell的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 60;}//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {// NSLog(@"didSelectRowAtIndexPath");//}#pragma mark 提交编辑操作时会调用这个方法(删除,添加)- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {// 删除操作if (editingStyle == UITableViewCellEditingStyleDelete) {// 1.删除数据[self.data removeObjectAtIndex:indexPath.row];// 2.更新UITableView UI界面// [tableView reloadData];[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];} else {// 添加操作// 1.添加数据int row = indexPath.row + 1;[self.data insertObject:@"新添加的数据" atIndex:row];// 2.更新UI界面//[tableView reloadData];NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationAutomatic];}}#pragma mark 决定tableview的编辑模式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {return _editingStyle;}#pragma mark 只有实现这个方法,编辑模式中才允许移动Cell- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {// NSLog(@"from(%i)-to(%i)", sourceIndexPath.row, destinationIndexPath.row);// 更换数据的顺序[self.data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];}#pragma mark - 公共方法#pragma mark 删除数据- (void)deleteData {_editingStyle = UITableViewCellEditingStyleDelete;// 开始编辑模式// self.tableView.editing = YES;// [self.tableView setEditing:YES];BOOL isEditing = self.tableView.isEditing;// 开启\关闭编辑模式[self.tableView setEditing:!isEditing animated:YES];}#pragma mark 添加数据- (void)addData {_editingStyle = UITableViewCellEditingStyleInsert;BOOL isEditing = self.tableView.isEditing;// 开启\关闭编辑模式[self.tableView setEditing:!isEditing animated:YES];}@end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法