重新排序时无法将UITableViewCell从其当前位置拖动 [英] Can't drag UITableViewCell from its current position when reorder

查看:58
本文介绍了重新排序时无法将UITableViewCell从其当前位置拖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使支持核心数据的UITableView具有重新排序的能力,在实现所有这些委托和提到的核心数据的某些技术之后,在这里,我发现了奇怪的行为.点击编辑按钮后,重新排序图标显示得很好,我可以点击它,出现阴影,但是当我尝试将其移到另一行时,我突然失去了焦点,单元格又移回了它的位置.有人知道这个问题的原因吗?

I'm trying to make my Core data backed UITableView have reorder ability, After implement all those delegate and some technique for core data mentioned here I found strange behavior. After tap edit button, reorder icon show just fine and I can tap on it, the shadow show up, but when I try to move it to other row, I suddenly lose the focus and the cell move back to its place. Have anyone know the cause of this problem ?

以下是显示问题的视频

http://www.youtube.com/watch?v= ugxuLNL7BnU& feature = youtu.be

这是我的代码

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-tableView:moveRowAtIndexPath:toIndexPath 中的

我尝试了下面的代码,只是一个空的实现,但是问题仍然存在,所以我不认为这不是问题.

in -tableView:moveRowAtIndexPath:toIndexPath I tried the code below and just an empty implementation, but the problem still exist, so I don't think this is not a problem.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    _changeIsUserDriven = YES;

    NSUInteger fromIndex = sourceIndexPath.row;
    NSUInteger toIndex = destinationIndexPath.row;

    NSMutableArray *newsArray = [[self.fetchedResultsController fetchedObjects] mutableCopy];
    News *news = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];

    [newsArray removeObject:news];
    [newsArray insertObject:news atIndex:toIndex];

    int i = 1;
    for (News *n in newsArray) {
        n.displayOrder = [NSNumber numberWithInt:i++];
    }

    [self saveContext];

    _changeIsUserDriven = NO;
}

FetchedResultController委托

FetchedResultController delegate

    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
    {
        if (_changeIsUserDriven) {
            return;
        }
        [self.tableView beginUpdates];
    }




       - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
                   atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
        {
            if (_changeIsUserDriven) {
                return;
            }
            switch(type) {
                case NSFetchedResultsChangeInsert:
                    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                    break;

                case NSFetchedResultsChangeDelete:
                    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
                    break;
            }
        }


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    if (_changeIsUserDriven) {
        return;
    }
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
    {
        if (_changeIsUserDriven) {
            return;
        }
        [self.tableView endUpdates];
    }

推荐答案

最后,我发现了问题所在,因为我使用的外部库IIViewDeckController消除了该问题.

Finally I found the problem its because external library that I use, IIViewDeckController, After remove that the problem go away.

这篇关于重新排序时无法将UITableViewCell从其当前位置拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆