【问题标题】:UITableView: move cell to a position to another with drag & dropUITableView:通过拖放将单元格移动到另一个位置
【发布时间】:2013-10-25 09:13:22
【问题描述】:

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

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

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

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

提前致谢。

【问题讨论】:

标签: ios uitableview drag-and-drop


【解决方案1】:

检查这个苹果文档链接

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

你需要管理条件

targetIndexPathForMoveFromRowAtIndexPath

canMoveRowAtIndexPath

【讨论】:

    【解决方案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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        试试这段代码是否正常。

         #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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-17
          • 2014-02-03
          • 1970-01-01
          • 1970-01-01
          • 2013-11-23
          • 2017-10-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多