jjzjj

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

coder 2024-01-25 原文

我有一个带有交互式单元格的 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 - UICollectionView 移动单元格在单击单元格后还原的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. ruby-on-rails - Prawn - 表格单元格内的链接 - 2

    我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  8. ruby - 单元测试文件 I/O 方法 - 2

    我对单元测试还是比较陌生。我用Ruby编写了一个类,它接受一个文件,在该文件中搜索给定的Regex模式,替换它,然后将更改保存回文件。我希望能够为此方法编写单元测试,但我不知道我将如何去做。有人能告诉我我们如何对处理文件i/o的方法进行单元测试吗? 最佳答案 看看这个HowdoIunit-testsavingfiletothedisk?基本上这个想法是一样的,文件系统是你的类的依赖。所以引入一个可以在你的单元测试中模拟的角色/接口(interface)(这样你在单元测试时就没有依赖性);角色中的方法应该是您从文件系统中需要的所有东西

  9. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  10. ruby - 尝试运行 minitest 单元测试时出错 - 2

    尝试使用rubytest/test_foo.rb运行minitest单元测试时出现以下错误:Warning:youshouldrequire'minitest/autorun'instead.Warning:oradd'gem"minitest"'before'require"minitest/autorun"'From:/home/emile/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/autorun.rb:15:```test_foo.rb看起来像这样:require'minitest/autorun'classTestFoo

随机推荐