Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 673
Clay Avatar asked Apr 27 '13 05:04

Clay


2 Answers

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...

Screenshot

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

like image 117
Prince Kumar Sharma Avatar answered Oct 18 '22 05:10

Prince Kumar Sharma


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.

like image 20
Clay Avatar answered Oct 18 '22 05:10

Clay