简体   繁体   中英

Merging UITableViewCells

I would like to be able to drag a cell over another cell, and when I release it would effectively "merge" the cells (display a different cell). I'm not sure if a UITableView is the best control or a UICollectionView . Does anyone have any thoughts on how to proceed?

May be I didn't get u properly but as I got, u want to merge cells by dragging so implement some code for you.Have a look at this.

If u Wanna something like this...

截图

use editing code like that :---

ViewDidLoad initializing array and set table to edit mode as:

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

datasources for tableView set as:->

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

Try this sampleCode

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

  1. Add a UILongPressGestureRecognizer to the UITableView
  2. On the UIGestureRecognizerStateBegan , use the [UITableView indexPathForRowAtPoint] in order to determine what cell is being "long pressed". You will turn this cell into the hover UITableViewCell .
  3. Set a reference variable to the corresponding model object.
  4. Use the UIGraphicsBeginImageContextWithOptions to render an image of the actual UITableViewCell contents. Create a UIImageView and add some custom chrome to it (shadows, etc.)
  5. Add the custom UIImageView as a subview to the UITableView, and remove the original selected pinned UITableViewCell (removeFromSuperview).
  6. On UIGestureRecognizerStateChanged , calculate the location of the hover UITableViewCell in the UITableView and animate the pinned cell. (You will also want to toggle the animation for the previously highlighted pinned cell).
  7. When the user releases the LongPress, you'll want to re-order the model array, remove the underlying pinned cell, and then reload the UITableView all in a beginUpdates call.

There are a few other things to watch out for such as scrolling when you hit a boundary condition, dropping the hover cell over the original location, etc. but that's the basic gist of it. Hopefully that helps.

I used Long press gesture for this. here is sample code

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

I think this is helpful for you. Take some code what you want from this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM