UITableView:通过拖放将单元格移动到另一个位置

我有一个有3个部分的表(第一部分,第二部分和第三部分)。 我想实现一种机制来通过拖放(在同一部分内或从一个部分到另一个部分)来改变单元格的位置。

示例1:在第一部分中,我有两行(sec1_row1和sec1_row2)。 在第二节我有一行(sec2_row1)通过拖放function我想用sec2_row1反转sec1_row2。 我想选择sec2_row1,将它拖到sec2_row1上然后放下它以使行反转。

我希望此function也适用于相同部分的行。

实现此function的最佳方法是什么?

提前致谢。

检查此Apple doc链接

https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html

你需要管理条件

targetIndexPathForMoveFromRowAtIndexPath

canMoveRowAtIndexPath

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { NSDictionary *section = [data objectAtIndex:sourceIndexPath.section]; NSUInteger sectionCount = [[section valueForKey:@"content"] count]; if (sourceIndexPath.section != proposedDestinationIndexPath.section) { NSUInteger rowInSourceSection = (sourceIndexPath.section > proposedDestinationIndexPath.section) ? 0 : sectionCount - 1; return [NSIndexPath indexPathForRow:rowInSourceSection inSection:sourceIndexPath.section]; } else if (proposedDestinationIndexPath.row >= sectionCount) { return [NSIndexPath indexPathForRow:sectionCount - 1 inSection:sourceIndexPath.section]; } // Allow the proposed destination. return proposedDestinationIndexPath; } 

有关更多信息,请查看以下链接

管理行的重新排序

如果你想阻止外部重新排序,那么请参考这个链接: 如何限制UITableView行重新排序到一个部分

您可以使用以下方法控制部分内和部分之间的行的移动

 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath { // Do not allow any movement between section if ( sourceIndexPath.section != proposedDestinationIndexPath.section) return sourceIndexPath; // You can even control the movement of specific row within a section. eg last row in a Section // Check if we have selected the last row in section if ( sourceIndexPath.row < sourceIndexPath.length) { return proposedDestinationIndexPath; } else { return sourceIndexPath; } // You can use this approach to implement any type of movement you desire between sections or within section } 

试试这段代码工作正常。

  #import "ViewController.h" @interface ViewController () { NSMutableArray *NameArray; } @property (weak, nonatomic) IBOutlet UITableView *NameListTableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _NameListTableView.delegate=self; _NameListTableView.dataSource=self; NameArray=[NSMutableArray arrayWithObjects:@"apple", @"b", @"c", @"d", @"e",@"f", @"g", @"h",@"i",@"j", @"k", nil]; [self.NameListTableView setEditing:YES animated:YES]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return NameArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId=@"NameCell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId]; if(cell!=nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId]; } NSString *abc=[NameArray objectAtIndex:indexPath.row]; cell.textLabel.text=abc; return cell; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleNone; } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { [NameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; [self.NameListTableView reloadData]; } @end