繁体   English   中英

单击一个单元格后,UICollectionView移动单元格还原

[英]UICollectionView move cells reverts after clicking a cell

我有一个包含交互式单元格的collectionview,我在单元格上使用了长按手势识别器,以便用户重新排列它们。

现在,重新排列本身可以正常工作,我遇到的问题是,如果单击单元格之一,它们会恢复到重新排列发生之前的位置,则一旦重新排列了单元格。

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

长按手势可以重新排列单元格。

-(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在click事件上重新加载单元格。

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

}

任何帮助将不胜感激。

编辑:[databaseclass 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数组中,则数组中数据的顺序将恢复为数据库查询返回的顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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