Merge UITableViewCells

I would like to be able to drag a cell over another cell, and when I let it go, it will effectively “change” the cells (display another cell). I am not sure what the UITableViewbest control is or UICollectionView. Anyone have thoughts on how to proceed?

+5
source share
3 answers

Here's the solution I used to develop it in pseudo code.

  • Add UILongPressGestureRecognizerto UITableView
  • On UIGestureRecognizerStateBeganuse [UITableView indexPathForRowAtPoint]to determine which cell is long pressed. You will turn this cell into a hover UITableViewCell .
  • Set the reference variable to the appropriate model object.
  • UIGraphicsBeginImageContextWithOptions UITableViewCell. UIImageView ( ..).
  • UIImageView UITableView UITableViewCell (removeFromSuperview).
  • UIGestureRecognizerStateChanged hover UITableViewCell UITableView . ( ).
  • LongPress, , , UITableView beginUpdates.

, , , , , .., . , .

+2

, , , , , , - . .

- ...

Screen shot

: ---

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

sampleCode

+4

.

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

, . - .

+1

All Articles