ios - 关于插入和删除单元格的 UITableView 动画问题

标签 ios objective-c nsmutablearray uitableview

我已经尝试过各种解决方案,但它们似乎不起作用。插入和删除操作后出现一致性异常。
目前我使用 reloadSection 方法作为临时解决方案,这提供了糟糕的动画体验。
如何使用 insertRowsWithAnimationdeleteRowsWithAnimation 这将在以下代码中产生正确的动画:

- (void) imageTapped:(id)sender {

    @try {

    UIButton *button = sender;
    NSInteger tag = button.tag;
        GroupNode *group = [[dictGroups objectForKey:@"0"] objectAtIndex:tag];
        NSMutableArray *arr = [[CommonModel shared]GetAllGroupNodesByTreeId:group.TreeId];

        BOOL isVisited = NO;
        GroupNode *nextNode;
        for(GroupNode *gp in arr)
        {
            if(!isVisited)
            {
                if([gp.Id isEqual:group.Id])
                {
                    isVisited = YES;
                }
            }
            else
            {
                nextNode = gp;
                break;
            }
        }

        if(![group.Indent isEqual:@"0"] && [nextNode.Indent intValue] == [group.Indent intValue] + 1)
        {
            if ([arrExpandedGroups containsObject:group.Id]) {
                [arrExpandedGroups removeObject:group.Id];
                NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
                NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];

                NSMutableArray *arr1 = [dictGroups objectForKey:@"0"];

                NSMutableArray *arrDelete = [[NSMutableArray alloc]init];
                BOOL isChild = YES;

                for(int i = 0; i< arr1.count;i++)
                {
                    GroupNode *gp = [arr1 objectAtIndex:i];
                    if(![group.Id isEqual:gp.Id])
                    {
                        if([arr1 indexOfObject:gp] < [arr1 indexOfObject:group])
                        {
                            [arrDelete addObject:gp];
                        }
                        else
                        {
                            if(([gp.Indent intValue] > [group.Indent intValue]) && !isChild)
                            {
                                [arrDelete addObject:gp];
                            }
                            else if(([gp.Indent intValue] <= [group.Indent intValue]))
                            {
                                [arrDelete addObject:gp];
                                isChild = NO;
                            }
                            else
                            {
                                [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
                            }
                        }
                    }
                    else
                    {
                        [arrDelete addObject:gp];

                        [dictExpand removeObjectForKey:[NSString stringWithFormat:@"%i", (int)button.tag]];
                        button.tag = [arrDelete indexOfObject:gp];
                        [dictExpand setObject:@"no" forKey:[NSString stringWithFormat:@"%i", (int)button.tag]];
                        for(NSString *key in dictExpand.allKeys)
                        {
                            if(![key isEqual:[NSString stringWithFormat:@"%i", (int)button.tag]])
                            {
                                if([group.Indent intValue] == 1)
                                    [dictExpand setObject:@"no" forKey:key];
                            }
                        }
                    }
                }

                [tableGroup beginUpdates];
                [dictGroups setObject:arrDelete forKey:@"0"];
                NSRange range = NSMakeRange(0, 1);
                NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
                [tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone];
                [tableGroup endUpdates];
            } else {

                if([group.Indent intValue] == 1)
                    [arrExpandedGroups removeAllObjects];
                [arrExpandedGroups addObject:group.Id];
                NSMutableArray *arrAdd = [NSMutableArray new];
                NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
                NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];

                NSMutableArray *arr1 = [dictGroups objectForKey:@"0"];

                BOOL isVisited = NO;
                BOOL isMainVisited = NO;
                int icount = 0;
                for(GroupNode *gp in arr)
                {
                    BOOL isPresentInArr1 = NO;
                    for(GroupNode *g in arr1)
                    {
                        if([g.Id isEqual:gp.Id] && ![group.Indent isEqual:@"1"])
                        {
                            isPresentInArr1 = YES;
                            break;
                        }
                    }

                    if(!isVisited && (([gp.Indent intValue] == 0 || [gp.Indent intValue] == 1) || isPresentInArr1))
                    {
                        [arrAdd addObject:gp];
                        [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
                        if([gp.Id isEqual:group.Id])
                        {
                            isVisited = YES;
                            [dictExpand removeObjectForKey:[NSString stringWithFormat:@"%i", (int)button.tag]];
                            button.tag = [arrAdd indexOfObject:gp];
                            [dictExpand setObject:@"yes" forKey:[NSString stringWithFormat:@"%i", (int)button.tag]];

                            for(NSString *key in dictExpand.allKeys)
                            {
                                if(![key isEqual:[NSString stringWithFormat:@"%i", (int)button.tag]])
                                {
                                    if([group.Indent intValue] == 1)
                                        [dictExpand setObject:@"no" forKey:key];
                                }
                            }
                        }
                    }
                    else if(isVisited)
                    {
                        if([gp.Indent intValue] > [group.Indent intValue] && ([gp.Indent intValue] == [group.Indent intValue] + 1) && !isMainVisited)
                        {
                            [arrAdd addObject:gp];
                            [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
                        }
                        else if([group.Indent intValue] > 1 && [gp.Indent intValue] == [group.Indent intValue] && !isMainVisited)
                        {
                            [arrAdd addObject:gp];
                            [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
                        }

                       else if([gp.Indent intValue] <= 1 && !isMainVisited)
                        {
                            [arrAdd addObject:gp];
                            [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
                            isMainVisited = YES;
                        }
                        else if([gp.Indent intValue] <= 1 && isMainVisited)
                        {
                            [arrAdd addObject:gp];
                            [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
                        }
                        else
                        {
                            for(GroupNode *gn in arr1)
                            {
                                if([gn.Id isEqual:gp.Id])
                                    [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]];
                            }
                        }
                    }

                    icount ++;
                }

                [dictGroups setObject:arrAdd forKey:@"0"];
                [tableGroup beginUpdates];
                NSRange range = NSMakeRange(0, 1);
                NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
                [tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone];
                [tableGroup endUpdates];
            }
        }
    }
    @catch (NSException *exception) {

    }
}  

我知道这是一个复杂的逻辑,但这就是它的工作原理。
arr - 所有数据库记录数组
arr1 - 所有显示的记录

编号的行数 :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int count = (int)[[dictGroups objectForKey:@"0"] count];
    return count;
}  

此数字始终与 imageTapped 函数中数组中的记录数相同。

最佳答案

当您将行添加到数组中时,也会在 TableView 中插入行。您立即重新加载了整个部分...这可能是个问题!(我不确定!!!)

在添加行时更新索引路径时添加:

[tableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

删除索引时是这样的:

[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

这可能会有所帮助,如果没有,请尝试更改代码中的以下行:

[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone]; 

到:

[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];

关于ios - 关于插入和删除单元格的 UITableView 动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151001/

相关文章:

ios - 在其他类中的 dismissViewController 之后设置 UILabel 的文本

ios - Quickblox 自定义对象数组的 NSPredicate

iphone - 如何创建 NSArray 的 Objective-C NSDictionary 而不重复使用相同的 NSArray?

ios - UITableViewCellAccessoryCheckmark 覆盖 iOS 7 上的单元格分隔符

ios - 为什么在 iOS 上构建依赖项时会在 "-reexport_framework"上出现链接器错误?

objective-c - iOS内存管理问题

objective-c - 将 NSMutableArray 或 NSMutableDictionary 传递给一个方法——你可以添加它吗?

ios - 以编程方式控制辅助 View 的高度到解析 TableView 中

iphone - 在 iOS 上收到通知时确定应用程序是否在前台运行

objective-c - iOS实时应用