UITableView的编辑步骤
1.让tableView处于编辑状态
2.协议设定
- 确定Cell是否处于编辑状态
- 设定cell的编辑样式
- 提交编辑状态
代码示例
- 让tableView处于编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
self.editButtonItem.title = editing ? @"完成" : @"编辑";
}
- 确定cell是否处于编辑状态
//是否能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- 设定cell的编辑样式
//设置编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _editStyle;
}
- 提交编辑状态
//提交编辑样式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 如果分组只剩一个元素,直接删除分组
NSMutableArray *array = self.dataDictionary[self.dataArray[indexPath.section]];
if (array.count == 1) {
//从数据源中删除分组
[self.dataDictionary removeObjectForKey:self.dataArray[indexPath.section]];
//删除索引
[self.dataArray removeObject:self.dataArray[indexPath.section]];
//删除UI
NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
[tableView deleteSections:set withRowAnimation:UITableViewRowAnimationFade];
}else{
//从数据源中删除元素
[array removeObjectAtIndex:indexPath.row];
//删除UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}else{
NSDictionary *dict = @{@"name":@"侯兆国",@"gender":@"男",@"phoneNumber":@"120",@"headerImg":@"侯兆国"};
MyClass *myClass = [MyClass myClassWithDic:dict];
NSMutableArray *array = self.dataDictionary[self.dataArray[indexPath.section]];
//插入数据index+1
[array insertObject:myClass atIndex:indexPath.row+1];
//修改UI
NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
[tableView insertRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
}
}
效果图:
示例图UITableView的移动步骤
- 实现协议:告诉tableView是否能够移动
- 实现移动方法
- 实现协议:告诉tableView是否能够移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
- 实现移动方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//根据原路径找到对应分组
NSMutableArray *array = self.dataDictionary[self.dataArray[fromIndexPath.section]];
//找到对应的内容
MyClass *myClass = array[fromIndexPath.row];
//删除原来的位置
[array removeObject:myClass];
//添加到新的位置
[array insertObject:myClass atIndex:toIndexPath.row];
}
- 防止不同分区之间的移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
}else{
return sourceIndexPath;
}
}
网友评论