UITableView编辑
编辑:
//1.让tableView进入编辑状态
- (void)deleteDataAction:(UIBarButtonItem *)barButton{ }
//2.设置可编辑区域
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ }
//3.设定编辑样式(删除或者添加)
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ }
//4.完成编辑(操作UI之前要先操作数据,所有的编辑都是先编辑数据)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ }
移动:
//1.让tableView进入编辑状态
- (void)moveDataAction:(UIBarButtonItem *)barButton{ }
//2.指定哪些可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ }
//3.完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//获取源分区数组
NSMutableArray *array = self.dataDict[self.group[sourceIndexPath.section]];
//找到对应的对象(要移动的元素)
self.student = array[sourceIndexPath.row];
//从原位置移除(同一分区的情况下在同一数组操作)
[array removeObject:_student];//相同的会全部移除
//添加到新位置(同一分区的情况下在同一数组操作)
[array insertObject:_student atIndex:destinationIndexPath.row];
//进行数据交换
}
//补充:限制是否可以跨区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
//如果源分区和目的分区是同一个地方,那么可以返回目的地点作为移动结果
if(sourceIndexPath.section == proposedDestinationIndexPath.section){
return proposedDestinationIndexPath;
}
//否则,滚回老家
return sourceIndexPath;
}
UITableViewController
//2.self.view 和 self.tableView 是同一个对象 , 但一般情况下我们使用 self.tableView, 原因是我们必须保证消息可以正确发送 ( 子类扩充了父类 )