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界面)