/**
创建编辑按钮
*/
- (void)createEdoitBtn{
UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(20,60,80,30)];
btn.backgroundColor = [UIColorredColor];
[btn setTitle:@"编辑"forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(edoitbtn:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
/**
编辑事件
@param btn 编辑按钮
*/
- (void)edoitbtn:(UIButton *)btn{
btn.selected = !btn.selected;
if (btn.selected ) {
//移除之前选中的内容
if (self.selectorPatnArray.count > 0) {
[self.selectorPatnArrayremoveAllObjects];
}
[btn setTitle:@"完成"forState:UIControlStateNormal];
//进入编辑状态
[self.wTableViewsetEditing:YESanimated:YES];
[self.wTableViewsetAllowsSelectionDuringEditing:YES];
}else{
[btn setTitle:@"编辑"forState:UIControlStateNormal];
[self.wTableViewsetEditing:NOanimated:YES];
}
}
//行的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[selfchangRowBackGroud:indexPath];
//选中数据
[self.selectorPatnArrayaddObject:self.dataSource[indexPath.row]];
NSLog(@"加入--self.selectorPatnArray:%@",self.selectorPatnArray);
}
//点击已选中的行调这个方法
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
//从选中中取消
if (self.selectorPatnArray.count > 0) {
[self.selectorPatnArrayremoveObject:self.dataSource[indexPath.row]];
}
NSLog(@"删除---self.selectorPatnArray:%@",self.selectorPatnArray);
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
returnYES;
}
//移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 需要的移动行
NSInteger Row1 = [sourceIndexPathrow];
// 获取移动某处的位置
NSInteger Row2 = [destinationIndexPathrow];
// 从数组中读取需要移动行的数据
id object = [self.dataSourceobjectAtIndex:fromRow];
// 在数组中移动需要移动的行的数据
[self.dataSourceremoveObjectAtIndex:Row1];
// 把需要移动的单元格数据在数组中,移动到想要移动的数据前面
[self.dataSourceinsertObject:objectatIndex:Row2];
}
//显示的状态。删除。增减。没有
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.wTableView.editing) {// 当进入编辑页面时就显示批量选择
returnUITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}else{
returnUITableViewCellEditingStyleDelete;//其他时候就左滑显示各种按钮
}
}
/**
* 左滑cell时展示多个不同的按钮
*/
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *action0 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"置顶"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
NSLog(@"点击了置顶");
// 收回左滑出现的按钮(退出编辑模式)
tableView.editing =NO;
}];
UITableViewRowAction *action1 = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
// 收回左滑出现的按钮(退出编辑模式)
tableView.editing =NO;
}];
return@[action1, action0];
}
更多详细代码:http://download.csdn.net/detail/qq_33646395/9754698