developer tip

-[UITableView _endCellAnimationsWithContext :]에서 어설 션 실패

optionbox 2020. 9. 8. 07:54
반응형

-[UITableView _endCellAnimationsWithContext :]에서 어설 션 실패


바라건대 이것은 빠른 수정이 될 것입니다. 나는 내가 계속 얻는 오류를 파악하려고 노력하고 있습니다. 오류는 아래에 나열되어 있으며 appdelagate는 그 아래에 있습니다.

도움을 주시면 감사하겠습니다.

감사

2012-04-12 21 : 11 : 52.669 Chanda [75100 : f803] --- Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:10372012-04-12 21 : 11 : 52.671 Chanda [75100 : f803] --- 잡히지 않은 예외 ' NSInternalInconsistencyException' 로 인해 앱 종료 , 이유 : 'Invalid update : invalid number of rows in section 0. 업데이트 후 기존 섹션에 포함 된 행 수 (2)는 업데이트 전 해당 섹션에 포함 된 행 수 (2)와 같아야합니다. 또는 해당 섹션에서 삽입 또는 삭제 된 행 수 (삽입 1 개, 삭제 0 개) 및 해당 섹션 안팎으로 이동 된 행 수 (0 개 이동, 0 개 이동)를 더하거나 뺀 값입니다. '

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize databaseName,databasePath; 

- (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
    self.databaseName = @"Customers.db";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    self.databasePath = [documentDir stringByAppendingPathComponent:self.databaseName];
    [self createAndCheckDatabase];

    return YES;
}

- (void)createAndCheckDatabase {
    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];

    if (success) return; 

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:self.databaseName];

    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

@end

이 코드 부분을 보여 주신 이유를 모르겠습니다. 귀하의 오류는 내가 가정하는 귀하의 코드 에서이 부분에 연결되어야합니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

Probably you are making a mistake in one of these data source methods. Currently it's impossible to say what exactly is wrong but I assume it could be something like: You are telling the table view in the numberOfRowsInSection you would like to have n rows reserved and setup and in the cellForRowAtIndexPath you then only handle n - 1 rows for example.

Sorry that this answer can't be as precise as it should be. If you show us your implementation of your data source it would be much easier to tell what's going on.


Like Sun Tzu said: it's best to win without fighting. In my case whenever I see this kind of error message (ie discrepancy between rows added deleted etc).. I don't even debug anything.. I simply avoid making that extra call where I reload the rows etc.. that's 99% of the cases where this error happens.

This is a common scenario where this bug happens: I have a UINavigationController and it has a UITableView, when I click on a row it pushes a new UITableView and so on. This error always happens to me when I pop the last UITableview and go back to the UITableView before it, at this point I make an unnecessary call to the loadIt function which basically inserts the rows and relaods the UITableView.

The reason this happens is because I erroneously place my loadIt function in viewDidAppear:animated rather than viewDidLoad. viewDidAppear:animated is called every time the UITableView is displayed, viewDidLoad is only called once.


When removing rows, remember that it also checks sections when updating, in:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView

If you want to remove a row that is the last item in a section you need to remove the whole section instead (otherwise it might get section count wrong and throw this exception).


Don't forget to update your array which determines numberOfRowsInSection. It needs to be updated before you animate and remove

We check if number of rows in section is 1 because we will have to delete the entire section.

Do correct me if anyone can make this answer clearer.

[self.tableView beginUpdates];
if ([tableView numberOfRowsInSection:indexPath.section] == 1) {

   [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
} else {

   [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
[self.tableView endUpdates];

I put each section elements in separated arrays. Then put them into another array( arrayWithArray). My solution here for this problem:

[quarantineMessages removeObject : message];
[_tableView beginUpdates];
if([[arrayWithArray objectAtIndex: indPath.section] count]  > 1)
{
    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indPath] withRowAnimation:UITableViewRowAnimationBottom];
}
else
{
    [_tableView deleteSections:[NSIndexSet indexSetWithIndex:indPath.section]
              withRowAnimation:UITableViewRowAnimationFade];
}
[_tableView endUpdates];

I had the same error.

I was using the following lines

UINib *myCustomCellNib = [UINib nibWithNibName:@"CustomNib" bundle:nil];
[tableView registerNib:myCustomCellNib forCellReuseIdentifier:@"CustomNib"];

to register the nib in the viewDidLoad method, since I had a different nib that was also associated with the same class. Hence, the line

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GBFBLoadingCell"];

was returning nil unless I registered the nib in the viewDidLoad.

My problem was that I forgot to set the identifier in the attributes inspector for my file "CustomNib.xib" and "CustomNib~iphone.xib". (Or more precisely, that I forgot to press enter after typing the identifier in the attribute inspector in XCode, so that the new name failed to save.)

Hope this helps.


If you're using an NSFetchedResultsController like me and updating data in a background thread, don't forget to begin and end updates in the delegate:

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

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

I Had the same error , which when trying with [tableView reloadData] was working fine . The error was actually in the line

[TabView insertRowsAtIndexPaths:indexPathsArray withRowAnimation:UITableViewRowAnimationRight];

When i tried to check the indexPath values , they weren't correct as required .

I fixed it by changing values in indexPathsArray .

Hope this helps .


Its could be one of UITableViewDataSource protocol methods

For

- tableView:numberOfRowsInSection:

it should return an integer equal to the sum or result of

-insertRowsAtIndexPaths:withRowAnimation: and/or -deleteRowsAtIndexPaths:withRowAnimation:

For

- numberOfSectionsInTableView:

it should return an integer equal to the sum or result of

-insertRowsAtIndexPaths:withRowAnimation: and/or -deleteSections:withRowAnimation:


I had the same problem with a Core Data base. If your using many FRC, you just need to reload the tableview inside each condition in numberOfSectionsInTableView.


I just had this happen to me while using Swift and a FRC backed data store to manage information. Adding a simple check to the delete operation to evaluate the current indexPath.section allowed me to avoid an extraneous call. I think I understand why this problem occurs... Basically I load a message into the top row whenever my dataset is empty. This creates an off by one issue as there is a faux row.

My Solution

... delete my entity, save the datastore and call reloadData on tableView
//next I add this simple check to avoid calling deleteRows when the system (wrongly) determines that there is no section.

       if indexPath.section > 0 {

        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .None)

            }

Simply check that you call [yourTableView reloadData]; after modify array of values.

참고URL : https://stackoverflow.com/questions/10134841/assertion-failure-in-uitableview-endcellanimationswithcontext

반응형