3

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

示例 1:在第一节中,我有两行(sec1_row1 和 sec1_row2)。在第二部分中,我通过拖放功能有一行(sec2_row1),我想用 sec2_row1 反转 sec1_row2。我想选择 sec2_row1,将其拖到 sec2_row1 上并将其放下,以便行倒置。

我希望此功能也可用于同一部分的行。

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

提前致谢。

4

4 回答 4

4

检查这个苹果文档链接

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

你需要管理条件

targetIndexPathForMoveFromRowAtIndexPath

canMoveRowAtIndexPath

于 2013-10-25T09:27:21.897 回答
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 行重新排序限制为节

于 2013-10-25T09:24:49.903 回答
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    
}
于 2014-07-27T00:46:02.757 回答
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
于 2017-02-22T08:37:37.920 回答