私はこれについてさまざまな解決策を見てきましたが、うまくいかなかったようです。挿入操作と削除操作の後に整合性例外が発生します。
当分の間、私はひどいアニメーション体験を与える一時的な解決策としてreloadSection
メソッドを使用しました。次のコードで正しいアニメーションになりますinsertRowsWithAnimation
とdeleteRowsWithAnimation
を使用する方法
:セルの挿入と削除に関するUITableViewのアニメーションの問題
- (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) {
}
}
私は、これは複雑なロジックで承知していますが、これはそれが動作する方法です。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = (int)[[dictGroups objectForKey:@"0"] count];
return count;
}
この数は常にの配列内のレコードの数と同じである機能をimageTapped:
編曲 - - すべてのデータベースレコードの配列
ARR1すべてのレコード
numberOfRowsInSectionを表示します。
我々は全体のポイントはreloadDataを使用しないときdeleteRowsAtIndexPaths後にreloadDataを使用しなければならないのはなぜ? – Nitish
配列数が1の場合、reloadDataは安全です。deleteRowsAtIndexPathsはクラッシュします。 –