ios - UICollectionView 移动单元格在单击单元格后还原

标签 ios objective-c uicollectionview uigesturerecognizer uicollectionviewlayout

我有一个带有交互式单元格的 Collection View ,我在单元格上使用长按手势识别器以允许用户重新排列它们。

现在重新排列本身运行良好,我遇到的问题是一旦单元格被重新排列,如果我单击其中一个单元格,它们将恢复到重新排列发生之前的位置。

我感觉这与数据源有关,但我不确定。

这是重新排列单元格的长按手势。

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

我正在使用 NSNotification 在点击事件时重新加载单元格。

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

如有任何帮助,我们将不胜感激。

编辑: [数据库类 getData] 方法 -

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

最佳答案

您没有说明您是如何获取数据的,但是这个 [databaseClass getData]; 表明它来自某个数据库表。如果您将数据库重新读入 people2 数组,则数组中数据的顺序将恢复为数据库查询返回的顺序。

关于ios - UICollectionView 移动单元格在单击单元格后还原,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22983042/

相关文章:

ios - 如何以编程方式修改 UIButton 的约束

ios - 如何将 UITableView 的顶部粘贴到 UISearchBar 的底部?

iphone - UIImageView旋转影响位置?

ios - 在 UICollectionViewFlowLayout 使其布局无效后重绘单元格

ios - 在另一个 UICollectionViewController 中添加一个 UICollectionViewController

ios - iOS从我在引导访问中运行的应用运行特定安装的游戏

ios - 如何在用户默认设置中保存特定的View Controller?

objective-c - UILabel 在 UITableViewCell 中变为 nil

ios - 如何使 AFNetworking 显示纯 HTTP 响应错误

ios - 使用以编程方式创建的 UICollectionView 时出现奇怪的绘图