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

3
我有一个由三个部分(section one、section two和section three)组成的表格。我想实现一个机制,通过拖放来改变单元格的位置(在同一个部分内或从一个部分到另一个部分)。
例1: 在第一部分中,我有两行(sec1_row1和sec1_row2)。在第二部分中,我有一行(sec2_row1)。通过拖放函数,我希望交换sec1_row2和sec2_row1的位置。我想选择sec2_row1,将其拖动到sec1_row2上并释放,以便行被倒置。
我希望这个功能也适用于同一部分的行。
最佳实现方式是什么?
提前感谢您。

1
使用内置的UITableView移动单元格功能 https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/moveRowAtIndexPath:toIndexPath:四处搜索... - Tim
4个回答

4

2
- (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行重新排序限制为一个部分


0

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

- (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. e.g 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    
}

0

尝试这段代码,它可以正常工作。

 #import "ViewController.h"

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    {
        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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接