简体   繁体   English

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

[英]UITableView: move cell to a position to another with drag & drop

i have a table with 3 section (section one, section two and section three). 我有一个有3个部分的表(第一部分,第二部分和第三部分)。 I would like implement a mechanism to change a cell's position with drag & drop (inside same section or from a section to another). 我想实现一种机制来通过拖放(在同一部分内或从一个部分到另一个部分)来改变单元格的位置。

Example 1: In section one i have two rows (sec1_row1 and sec1_row2). 示例1:在第一部分中,我有两行(sec1_row1和sec1_row2)。 In section two i have one row (sec2_row1) by drag & drop function i would like invert the sec1_row2 with sec2_row1. 在第二节我有一行(sec2_row1)通过拖放功能我想用sec2_row1反转sec1_row2。 I would like select the sec2_row1, drag it on sec2_row1 and drop it so the rows are inverted. 我想选择sec2_row1,将它拖到sec2_row1上并将其删除,以便反转行。

I would like that this feature will be also available for rows of same section. 我希望此功能也适用于相同部分的行。

What is the best way to implement this feature? 实现此功能的最佳方法是什么?

Thanks in advance. 提前致谢。

Check this apple doc link 查看此Apple doc链接

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

you need to manage conditions in 你需要管理条件

targetIndexPathForMoveFromRowAtIndexPath targetIndexPathForMoveFromRowAtIndexPath

canMoveRowAtIndexPath 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;
  }

For further information check below link: 有关详细信息,请查看以下链接

Managing the Reordering of Rows 管理行的重新排序

If you want to prevent re-ordering outside section then please refer this link: How to limit UITableView row reordering to a section 如果您想阻止外部重新排序,请参考此链接: 如何限制UITableView行重新排序到一个部分

You can control the movement of rows within section and between section using below approach 您可以使用以下方法控制区域内和区域之间的行的移动

- (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    
}

Try this code is working fine. 试试这段代码工作正常。

 #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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM