插入和删除单元格时出现UITableView动画问题

时间:2016-03-22 09:48:35

标签: ios objective-c nsmutablearray uitableview

我已经完成了各种解决方案,但它们似乎没有用。插入和删除操作后,我得到一致性异常 暂时我使用.setBeaconLayout("s:0-1=feaa,m:2-2=10,p:3-3:-41,i:4-20v")); 方法作为临时解决方案,提供了糟糕的动画体验 如何使用reloadSectioninsertRowsWithAnimation将导致以下代码中的正确动画:

deleteRowsWithAnimation

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

numberOfRowsInSection

- (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) {

    }
}  

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

2 个答案:

答案 0 :(得分:1)

在数组中添加行时,也会在表视图中插入行。你正在重新加载整个部分...可能这可能是一个问题!(我不确定!!!)

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

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

这是删除索引时的内容:

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

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

[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone]; 

为:

[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];

答案 1 :(得分:0)

首先从tableview数组中删除该对象,然后使用

[table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
[table reloadData];