UITableView编辑
数据准备
- (void)initializeData{
NSArray *array = @[@"0", @"1", @"2", @"3", @"添加"];
NSArray *array2 = @[@"0", @"1", @"2", @"3", @"4", @"添加"];
self.firstArray = [NSMutableArray arrayWithArray:array];
self.secondArray = [NSMutableArray arrayWithArray:array2];
}
添加编辑按钮
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(barButtonClick:)];
self.navigationItem.rightBarButtonItem = barButtonItem;
[barButtonItem release];
开启TableView的编辑状态,按钮实现方法
- (void)barButtonClick:(UIBarButtonItem *)barButtonItem{
[self.tableview setEditing:!self.tableview.editing animated:YES];
if (self.tableview.editing == YES) {
barButtonItem.title = @"完成";
}else{
barButtonItem.title = @"编辑";
}
}
允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
指定编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
if ([self.firstArray[indexPath.row] isEqualToString:@"添加"]) {
return UITableViewCellEditingStyleInsert;
}
}else{
if ([self.secondArray[indexPath.row] isEqualToString:@"添加"]) {
return UITableViewCellEditingStyleInsert;
}
}
return UITableViewCellEditingStyleDelete;
}
根据编辑的样式 和索引 去完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.firstArray removeObjectAtIndex:indexPath.row];
[self.tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}else {
[self.firstArray insertObject:@"绯村拔刀斋" atIndex:indexPath.row];
[self.tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}
}else {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.secondArray removeObject:self.secondArray[indexPath.row]];
[self.secondArray removeObjectAtIndex:indexPath.row];
}else {
[self.secondArray insertObject:@"浪客剑心" atIndex:indexPath.row];
[self.tableview insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}
[self.tableview reloadData];
}
}
编辑步骤总结
- 开启TableView的编辑状态
- 允许哪个分区或者哪个分区的哪行是可以编辑的 (默认都能编辑)
- 指定可以编辑的样式:删除 or 添加
- 完成编辑 (提交编辑):
- 操作数据源数组 (添加 或者 删除)
- 2.刷新UI界面
移动
开启编辑状态 与上面一样,移动也是编辑的一种
允许哪个分区的哪一行 可以编辑canMove
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
完成移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
if (destinationIndexPath.section == sourceIndexPath.section) {
if (sourceIndexPath.section == 0) {
NSString *str = self.firstArray[sourceIndexPath.row];
[self.firstArray removeObjectAtIndex:sourceIndexPath.row];
[self.firstArray insertObject:str atIndex:destinationIndexPath.row];
[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]
;
}else{
NSString *str = self.secondArray[sourceIndexPath.row];
[self.secondArray removeObjectAtIndex:sourceIndexPath.row];
[self.secondArray insertObject:str atIndex:destinationIndexPath.row];
[tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}
}else{
}
}
限制跨区移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
NSLog(@"我执行了");
if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
return proposedDestinationIndexPath;
}else{
return sourceIndexPath;
}
}
移动步骤总结
- 开启编辑状态
- 允许哪个分区的哪一行 可以编辑canMove
- 完成移动(1.操作数据源数组 2.刷新UI界面)