サーバーからのplistで更新しているUITableViewがあります。次の方法でテーブルビューにセクションを挿入および削除したいと思います。
[global.tableVC.tableViewOutlet insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
[global.tableVC.tableViewOutlet deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
問題は、私が持っているさまざまなセクションにも多くの行があるため、テーブルビューを更新すると常にこのエラーが発生することです:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2380.17/UITableView.m:1070
2013-05-11 13:16:39.946 uninkd[19619:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 3. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
ここで、突然のセクション 3 のすべての行が 1 ではなく 0 になっていることがわかりますが、それを処理する方法がよくわかりません。
すべてのセクションをループして、セクションに行があるかどうかを確認してから、それらの行を削除して下に挿入する必要がありますか? それらを移動しますか?
これは、テーブルビューを更新するための私の完全なコードです:
[global.tableVC.tableViewOutlet beginUpdates];
// Update data
NSString *manifestPlistPath = [global.documentsDirectory stringByAppendingPathComponent:@"Manifest.plist"];
[plist writeToFile:manifestPlistPath atomically:YES];
// Insert new section
[global.tableVC.tableViewOutlet insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
[global.tableVC.tableViewOutlet endUpdates];
問題と cellForRowAtIndexPath による更新:
-(NSMutableArray *)issues{
// 1. Initiate issues array
_issues = [[NSMutableArray alloc] init];
// 2. Define and set magazine and foreword
NSString *magazinePath = [[NSBundle mainBundle] pathForResource:@"Magazine" ofType:@"plist"];
NSString *forewordPath = [[NSBundle mainBundle] pathForResource:@"Foreword" ofType:@"plist"];
// 3. If we have any issues, then reset the magazine and foreword path
if (kLibrary.issues.count) {
// A. Get the last issue
NKIssue *lastIssue = [kLibrary.issues lastObject];
// B. Redefine the magazine and foreword path
magazinePath = [[lastIssue.contentURL path] stringByAppendingPathComponent:@"Magazine.plist"];
forewordPath = [[lastIssue.contentURL path] stringByAppendingPathComponent:@"Foreword.plist"];
}
// 5. If file exists at that path, then set the manifestplistdic
if ([self manifest].count){
// B. Add the individual issues to the issues global
for (NSMutableDictionary __strong *issue in [self manifest]){
NSString *issueName = [[issue objectForKey:@"Issue"] objectForKey:@"Name"];
NKIssue *libraryIssue = [kLibrary issueWithName:issueName];
if (libraryIssue) {
NSString *issuePath = [[libraryIssue.contentURL path] stringByAppendingPathComponent:@"Issue.plist"];
issue = [[NSMutableDictionary alloc] initWithContentsOfFile:issuePath];
}
[_issues addObject:issue];
}
}
// 6. Add the magazine and foreword
[_issues insertObject:[[NSDictionary alloc] initWithContentsOfFile:magazinePath] atIndex:0];
[_issues addObject:[[NSDictionary alloc] initWithContentsOfFile:forewordPath]];
// 7. Return the array
return _issues;
}
- (DTAttributedTextCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
return (DTAttributedTextCell *)[self tableView:tableView preparedCellForIndexPath:indexPath];
}
- (DTAttributedTextCell *)tableView:(UITableView *)tableView preparedCellForIndexPath:(NSIndexPath *)indexPath{
// 1. Get cell cache if cell has already been created
CustomCellView *cell = [global rowAtIndexPath:indexPath];
// 2. Create cell if it doesn't exist
if (!cell){
// A. Define cell string (for first row in buy section
NSString *cellString = @"";
// C. If we have articles, then reset cell string
if([global sectionAtSection:indexPath.section].bought){
// A. Find the right issue and article
NSDictionary *article = [global articleAtIndexPath:indexPath];
// B. Set the cell string and put it into an attributed string
cellString = [NSString stringWithFormat:@"%@ %@ %@", [article objectForKey:@"Headline"], [article objectForKey:@"Byline"], [article objectForKey:@"Lead"]];
}
// D. Create the cell from the cell identifier
cell = [[CustomCellView alloc] initWithIndexPath:indexPath title:cellString];
// E. Cache the cell, if there is a cache
[_rowCache setObject:cell forKey:[global cellKeyFromIndexPath:indexPath]];
}
// 3. Return the cell
return cell;
}