6

我希望能够将一个单元格拖到另一个单元格上,当我释放它时,它会有效地“合并”这些单元格(显示不同的单元格)。我不确定 aUITableView是最好的控制还是UICollectionView. 有人对如何进行有任何想法吗?

4

3 回答 3

4

可能是我没有正确理解你,但正如我得到的那样,你想通过拖动来合并单元格,所以为你实现一些代码。看看这个。

如果你想要这样的东西......

截屏

使用这样的编辑代码:---

ViewDidLoad 初始化数组并将表设置为编辑模式:

fruitsArray=[[NSMutableArray alloc] initWithObjects:@"Apple",@"Banana",@"Mango",@"Guava",@"PineApple",@"Watermelon",@"Grapes",@"GroundNut",@"Muskmelon",@"Orange",@"Cherry",nil];
[tblView setEditing:YES];

tableView 的数据源设置为:->

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    if (sourceIndexPath.row!=destinationIndexPath.row) {
        NSString *sourceString=[fruitsArray objectAtIndex:sourceIndexPath.row];
        NSString *destinationString=[fruitsArray objectAtIndex:destinationIndexPath.row];

        destinationString=[destinationString stringByAppendingFormat:@",%@",sourceString];

        [fruitsArray replaceObjectAtIndex:destinationIndexPath.row withObject:destinationString];
        [fruitsArray removeObjectAtIndex:sourceIndexPath.row];

        [tblView reloadData];
    }
}

试试这个示例代码

于 2013-07-22T13:57:56.903 回答
2

这是我用伪代码开发它的解决方案。

  1. UILongPressGestureRecognizer向 UITableView添加一个
  2. 在 上UIGestureRecognizerStateBegan,使用[UITableView indexPathForRowAtPoint]来确定正在“长按”的单元格。您将把这个单元格变成悬停 UITableViewCell
  3. 为相应的模型对象设置一个引用变量。
  4. 使用UIGraphicsBeginImageContextWithOptions来呈现实际 UITableViewCell 内容的图像。创建一个UIImageView并向其添加一些自定义 chrome(阴影等)
  5. 将自定义的 UIImageView 作为子视图添加到 UITableView 中,并移除原来选中的pinned UITableViewCell (removeFromSuperview)。
  6. 在 上UIGestureRecognizerStateChanged,计算悬停 UITableViewCell在 中的位置并为固定UITableView单元格设置动画。(您还需要切换先前突出显示的固定单元格的动画)。
  7. 当用户释放 LongPress 时,您需要重新排序模型数组,删除底层固定单元格,然后在调用中重新加载UITableView所有内容。beginUpdates

还有一些其他的事情需要注意,例如当你遇到边界条件时滚动,将悬停单元格放在原始位置等,但这是它的基本要点。希望这会有所帮助。

于 2013-07-25T02:58:24.460 回答
1

我为此使用了长按手势。这是示例代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
                longPress.delegate = self;
                [cell addGestureRecognizer:longPress];

    }

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
        if([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
            [gestureRecognizer addTarget:self action:@selector(moveRight:)];
        }
        return YES;

        if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
            [gestureRecognizer addTarget:self action:@selector(tapAction:)];
        }
        return YES;
    }

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
        return YES;
    }


- (void)moveRight:(UILongPressGestureRecognizer *)sender {

   /*if (sender.view.tag==4)
    {
        [super setEditing:YES animated:YES];
        [self.Table4 setEditing:YES animated:YES];
    }
    else
    {
        */

    UIView *view = sender.view;

    int t = sender.view.tag;
     CGPoint point = [sender locationInView:view.superview];

    NSLog(@"its added %f", point.x);
    NSLog(@"its added %f", point.y);
   // view.frame = CGRectMake(sender.view.frame.origin.x, sender.view.frame.origin.y+200,200, 30);
    CGPoint center = view.center;
    center.x = point.x ;
    center.y = point.y ;
    view.center = center;
    [self.view addSubview:view];



  //  int x = Table4.frame.origin.x;
   // int y = Table4.frame.origin.y;

    if (sender.state == UIGestureRecognizerStateChanged) {
        CGPoint center = view.center;
        center.x += point.x - _priorPoint.x;
        center.y += point.y - _priorPoint.y;

       NSLog(@"its Center %f", center.x);


        if (center.x > Table4.frame.origin.x  && 
            center.x < Table4.frame.origin.x  + 
            Table4.frame.size.width &&
            center.y > Table4.frame.origin.y  && 
            center.y < Table4.frame.origin.y  + 
            Table4.frame.size.height
            ) 
        {
            int count = [self.rowdata count];

            if (t>=100 && t<200)
            {
                t=t-100;
                 [self.rowdata insertObject:[listData objectAtIndex:t] atIndex:count];
            }
            else if (t>=200 && t<300)
            {
                 t=t-200;
                 [self.rowdata insertObject:[listData2 objectAtIndex:t] atIndex:count];
            }
            else
            {
                  t=t-300;
                 [self.rowdata insertObject:[listData3 objectAtIndex:t] atIndex:count];
            }           

            //[self.rowdata addObject:[listData objectAtIndex:t]];
            //[sender release];
            [Table4 reloadData];
            [Table1 reloadData];
            [Table2 reloadData];
            [Table3 reloadData];
        }

        view.center = center;
    //}


        _priorPoint = point;
    }
}

我认为这对你有帮助。从中获取一些您想要的代码。

于 2013-07-23T04:34:45.663 回答