UITableView的moveRowAtIndexPath的代码

暂时只是实现同一个section里的移动,要跨section移动的话,稍微懂点脑筋就行~

代码:

  1. - (void)tableView:(UITableView *)tableViewmoveRowAtIndexPath:(NSIndexPath *)fromIndexPathtoIndexPath:(NSIndexPath *)toIndexPath {
  2.     if (fromIndexPath != toIndexPath) {
  3.         id object = [self.dataMutableArray objectAtIndex:fromIndexPath.row];
  4.         [object retain];
  5.         [self.dataMutableArray removeObjectAtIndex:fromIndexPath.row];
  6.         if (toIndexPath.row > [self.dataMutableArray count]) {
  7.             [self.dataMutableArray addObject:object];
  8.         }
  9.         else {
  10.             [self.dataMutableArray insertObject:object atIndex:toIndexPath.row];
  11.         }
  12.         [object release];
  13.     }
  14. }
  15.  
  16. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  17.     return YES;
  18. }