如何拖动UITableViewCell并将其拖放到另一个UITableView?

abh*_*bhi 5 iphone ios4 ios ios5

我只能获得部分输出,实现了拖放的主要功能,但是它崩溃了一些时间并且显示错误作为索引超出范围.

我使用平移手势控制器来拖放UITableView单元格,我也想知道如何通过使用触摸开始,触摸移动和触摸结束方法来完成.

这是我的示例代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    pathNumber=indexPath.row;
    cell = [tableView cellForRowAtIndexPath:indexPath];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    [cell addGestureRecognizer:panRecognizer];


    if (cell.accessoryType==UITableViewCellAccessoryNone)
    {

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        if(tableView==tableView1)
        {
            [selectedCells addObject:[array objectAtIndex:indexPath.row]];
            NSLog(@"indexpath-%d",indexPath.row);
            [arrayOfPaths addObject:indexPath];
            NSLog(@"----%@",selectedCells);
            NSLog(@"----======%@",arrayOfPaths);
        }
        else
        {
            [selectedCells1 addObject:[array1 objectAtIndex:indexPath.row]];
            NSLog(@"indexpath-%d",indexPath.row);
            [arrayOfPaths1 addObject:indexPath];
            NSLog(@"====%@",selectedCells1);
            NSLog(@"----======]]]]]%@",arrayOfPaths1);
        }
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        if(tableView==tableView1)
        {
            [selectedCells removeObject:[array objectAtIndex:indexPath.row]];
            [arrayOfPaths removeObject:indexPath];
            NSLog(@"----%@",selectedCells);
            NSLog(@"----======remove%@",arrayOfPaths);
        }
        else
        {
            [selectedCells1 removeObject:[array1 objectAtIndex:indexPath.row]];
            [arrayOfPaths1 removeObject:indexPath];
            NSLog(@"====%@",selectedCells1);
            NSLog(@"----======remove1%@",arrayOfPaths1);
        }
    }
}

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
    CGPoint translation = [panRecognizer translationInView:self.view];
    CGPoint cellViewPosition = cell.center;
    cellViewPosition.x += translation.x;
    cellViewPosition.y += translation.y;

    cell.center = cellViewPosition;
    [self.view bringSubviewToFront:cell];
    [panRecognizer setTranslation:CGPointZero inView:self.view];
    NSArray * arr=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:pathNumber inSection:0]];
    NSArray * arr1=[NSArray arrayWithObject:[NSIndexPath indexPathForRow:pathNumber inSection:0]];

    if(panRecognizer.state==UIGestureRecognizerStateBegan)
    {
        position1=[panRecognizer locationInView:self.view];
    }
    if(panRecognizer.state==UIGestureRecognizerStateEnded)
    {
        position = [panRecognizer locationInView:self.view];

        NSLog(@"%f %f",position.x,position.y);

        if(CGRectContainsPoint(tableView2.frame, position))
        {
            NSLog(@"1-2");
            if(position.x>=tableView2.frame.origin.x && position1.x >= tableView1.frame.origin.x  && position1.x <= tableView1.frame.size.width)
            {
                if([selectedCells count] ==1)
                {
                    [tableView2 beginUpdates];
                    [tableView2 insertRowsAtIndexPaths:arr1 withRowAnimation:UITableViewRowAnimationTop];
                    [array1 insertObject:[array objectAtIndex:pathNumber] atIndex:pathNumber];
                    d=[array1 count];
                    [tableView2 endUpdates];

                    [tableView1 beginUpdates];
                    [tableView1 deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationTop];
                    [array removeObjectAtIndex:pathNumber];
                    c=[array count];
                    [tableView1 endUpdates];
                }
                else
                {
                    [tableView2 beginUpdates];
                    [tableView2 insertRowsAtIndexPaths:arrayOfPaths withRowAnimation:UITableViewRowAnimationTop];
                    for(int j=0;j<[arrayOfPaths count];j++)
                    {
                        NSIndexPath * tempIndexPath = (NSIndexPath *)([arrayOfPaths objectAtIndex:j]);
                        [array1 insertObject:[selectedCells objectAtIndex:j] atIndex:tempIndexPath.row];
                    }
                    d=[array1 count];
                    NSLog(@"%d", d);
                    [tableView2 endUpdates];

                    [tableView1 beginUpdates];

                    [tableView1 deleteRowsAtIndexPaths:arrayOfPaths withRowAnimation:UITableViewRowAnimationTop];
                    for(int j = 0;j < [arrayOfPaths count]; j++)
                    {
                        NSIndexPath * temp = (NSIndexPath *)[arrayOfPaths objectAtIndex:j];
                        [array removeObjectAtIndex:temp.row];
                    }
                    c=[array count];
                    [tableView1 endUpdates];


                }
                [selectedCells removeAllObjects];
                [arrayOfPaths removeAllObjects];
            }
            else
            {
                NSLog(@"else");
            }

        }

        else if(CGRectContainsPoint(tableView1.frame, position))
        {
            NSLog(@"hi-2");
            if(position.x>=tableView1.frame.origin.x && position1.x >= tableView2.frame.origin.x  && position1.x <= tableView2.frame.size.width + tableView2.frame.origin.x)
            {
                if([selectedCells1 count] == 1)
                {
                    [tableView1 beginUpdates];
                    [tableView1 insertRowsAtIndexPaths:arr1 withRowAnimation:UITableViewRowAnimationTop];
                    [array insertObject:[array1 objectAtIndex:pathNumber] atIndex:pathNumber];
                    c=[array count];
                    [tableView1 endUpdates];

                    [tableView2 beginUpdates];
                    [tableView2 deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationTop];
                    [array1 removeObjectAtIndex:pathNumber];
                    d=[array1 count];
                    [tableView2 endUpdates];
                }
                else
                {
                    [tableView1 beginUpdates];
                    [tableView1 insertRowsAtIndexPaths:arrayOfPaths1 withRowAnimation:UITableViewRowAnimationTop];
                    for(int j=0;j<[arrayOfPaths1 count];j++)
                    {
                        NSIndexPath * tempIndexPath = (NSIndexPath *)([arrayOfPaths1 objectAtIndex:j]);
                        [array insertObject:[selectedCells1 objectAtIndex:j] atIndex:tempIndexPath.row];
                    }
                    c=[array count];
                    NSLog(@"%d", c);
                    [tableView1 endUpdates];

                    [tableView2 beginUpdates];

                    [tableView2 deleteRowsAtIndexPaths:arrayOfPaths1 withRowAnimation:UITableViewRowAnimationTop];
                    for(int j = 0;j < [arrayOfPaths1 count]; j++)
                    {
                        NSIndexPath * temp = (NSIndexPath *)[arrayOfPaths1 objectAtIndex:j];
                        [array1 removeObjectAtIndex:temp.row];
                    }
                    d=[array1 count];
                    [tableView2 endUpdates];

                }
                [selectedCells1 removeAllObjects];
                [arrayOfPaths1 removeAllObjects];

            }
            else
            {
                NSLog(@"else1");
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 0

您可以获取您正在触摸的单元格。当您拥有该单元格时,您可以将它们的值存储在必须在“.h”上声明的临时单元格上,并从第一个表 xc2xb4s 数据源中删除。

\n\n

当您检测到触摸已结束时,如果触摸在其中一个表上结束,则将临时单元添加到其数据源,并进行重新加载数据。\n否则,如果触摸没有在表边界之一上结束\xc2\xb4t ,将临时单元格添加到\n原始表中。

\n\n

为了提高视觉功能,您可以在触摸坐标上显示 UITableViewCell。

\n\n

所以,一步一步:

\n\n
    \n
  • 在你的 ViewController 中声明:UITableviewCell * tempCell

  • \n
  • 在代码中的某个位置初始化“tempCell”tempCell = [[UITableViewCell alloc]init]

  • \n
  • 使用该方法: \n - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath\n{\n//Here you have selected row number;\n}\n您可以获取选定的行,以便可以从数据源恢复单元格值。\n将该值存储在“tempCell”上,并将它们从表\xc2\xb4s 数据源中删除。

  • \n
  • 如果您想在触摸时显示单元格,您可以将 UITableViewCell 添加到 ViewController.view 中。

  • \n
  • 随着- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;你获得触摸坐标。有了这些坐标,您可以检查它们是否在您的表格之一上。\n如果坐标属于您的表格之一,请添加到其数据源,清理“tempCell”(如果您在 ViewController 上显示,则删除 cellView\ xc2\xb4s 视图)\n最后执行[tableView reloadData];。\n否则,如果坐标\xc2\xb4t 不属于您的表,请将单元格添加到原始数据源并清理“tempCell”。

  • \n
\n\n

对不起,我的英语不好 :(

\n