UITableView:使用drag& amp;移动单元格到另一个位置下降

时间:2013-10-25 09:13:22

标签: ios uitableview drag-and-drop

我有一个有3个部分的表(第一部分,第二部分和第三部分)。我想实现一个机制来改变一个单元格的位置与drag&掉落(在同一部分内或从一个部分到另一部分)。

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

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

实施此功能的最佳方法是什么?

提前致谢。

4 个答案:

答案 0 :(得分:4)

检查此苹果文档链接

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

您需要管理

中的条件

targetIndexPathForMoveFromRowAtIndexPath

canMoveRowAtIndexPath

答案 1 :(得分: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;
  }

有关详细信息,请查看以下链接:

Managing the Reordering of Rows

如果您想阻止外部重新订购,请参阅此链接:How to limit UITableView row reordering to a section

答案 2 :(得分: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    
}

答案 3 :(得分: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