Question

I have a collectionview with interactive cells, I am using a long press gesture recognizer on the cells to allow the user to re-arrange them.

Now the re-arrange itself is working perfectly the issue I have is once the cells have been re-arranged if I click on one of the cells they revert back to the positions they were in before the re-arrange occured.

I have a feeling this has something to do with the datasource but im not sure.

This is the long press gesture that re-arranges the cells.

-(IBAction)longPressGestureRecognized:(id)sender {

UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
UIGestureRecognizerState state = longPress.state;

CGPoint location = [longPress locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location];

static UIView *snapshot = nil;
static NSIndexPath *sourceIndexPath = nil;

switch (state) {
    case UIGestureRecognizerStateBegan: {
        if (indexPath) {
            sourceIndexPath = indexPath;

            UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];

            snapshot = [self customSnapshotFromView:cell];

            CGPoint center = cell.center;
            snapshot.center = center;
            snapshot.alpha = 0.0;
            [self.collectionView addSubview:snapshot];
            [UIView animateWithDuration:0.25 animations:^{


                snapshot.center = center;
                snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);
                snapshot.alpha = 0.98;

                cell.hidden = YES;
            } completion:nil];
        }
        break;
    }
    case UIGestureRecognizerStateChanged: {
        CGPoint center = snapshot.center;
        center.y = location.y;
        snapshot.center = center;

        if (indexPath && ![indexPath isEqual:sourceIndexPath]) {
            [people2 exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];
            [self.collectionView moveItemAtIndexPath:sourceIndexPath toIndexPath:indexPath];
            sourceIndexPath = indexPath;
        }
        break;
    }
    default: {
        UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:sourceIndexPath];
        [UIView animateWithDuration:0.25 animations:^{

            snapshot.center = cell.center;
            snapshot.transform = CGAffineTransformIdentity;
            snapshot.alpha = 0.0;
            cell.hidden = NO;

        } completion:^(BOOL finished){
            [snapshot removeFromSuperview];
            snapshot = nil;



        }];
        sourceIndexPath = nil;
        break;
    }
}

}

I am using NSNotification to reload the cells on a click event.

-(void)handleReload:(NSNotification *)notification {
NSLog(@"Notification Recieved");
people2 = [databaseClass getData];
[self.collectionView reloadData];

}

Any help would be hugely appreciated.

EDIT: The [databaseclass getData] method -

+(NSMutableArray*)getData {
[self databaseInit];

peopleArray = [[NSMutableArray alloc] init];

if (sqlite3_open(dbpath, &peopleDB)== SQLITE_OK)
{

    NSString *selectSQL = @"SELECT ID, NAME, POINTS, COLOUR FROM PEOPLE";
    sqlite3_prepare_v2(peopleDB,[selectSQL UTF8String],-1,&statement,NULL);

        while (sqlite3_step(statement)== SQLITE_ROW)
        {
            PersonObject *newPerson = [[PersonObject alloc]init];
            newPerson.ID = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]integerValue];
            newPerson.name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
            newPerson.points = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] integerValue];
            newPerson.colour = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
            [peopleArray addObject:newPerson];

        }

    }

    sqlite3_finalize(statement);
    sqlite3_close(peopleDB);

return peopleArray;

}

Was it helpful?

Solution

You don't show how you are getting your data, but this [databaseClass getData]; suggests it is from some database table. If you are re-reading the database into the people2 array then the order of the data in the array will revert to the order returned by your database query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top