gpt4 book ai didi

iphone - 关于 UITableView 和 deleteRowsAtIndexPaths 的问题

转载 作者:搜寻专家 更新时间:2023-10-30 20:22:13 25 4
gpt4 key购买 nike

我关于 stackoverflow 的第一个问题 (<< n00b)。我正在处理我的第一个涉及 UITableViews 和 plist 的项目,但我遇到了问题。

属性列表由一个包含 3 个部分/类别(每个一个数组)的字典以及每个部分/类别中的多个条目组成。

列表加载得很好。在我尝试从列表中删除单个条目之前,问题不会发生。

这是我的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

//make array of the objects and remove the selected object from that array.
NSMutableArray *delArray = [faves objectForKey:[entries objectAtIndex:indexPath.section]];
[delArray removeObjectAtIndex:indexPath.row];

//set entries to the value of the array, now sans the removed object.
self.entries = delArray;
[delArray release];

//write the updated entries to the plist file.
NSArray *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [rootPath objectAtIndex:0];
NSString *plistFile = [docsPath stringByAppendingPathComponent:@"Data.plist"];
[faves writeToFile:plistFile atomically:YES];

//update the actual tableview. This is where things go awry.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; //ends up trying to load the sections instead of the entries = wrong number of list items returned = crash!

//[tableView reloadData]; //works if I only use this line, but the list updates wrong, showing each entry as a section header.
}
}

条目已从 plist 中正确删除,但表无法正确更新并导致应用程序崩溃。我在下面包含了错误代码。

2011-09-23 18:40:19.732 MyApp[10314:b303] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:974
2011-09-23 18:40:19.734 MyApp[10314:b303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (5) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

第一个数字 (5) 与受影响的类别中应该保留的记录数一致,但第二个数字 (3) 指的是类别数。正如我上面所说,该条目已从 plist 中删除 - 而不是表。

最佳答案

考虑以下两种方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [entries count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key = [entries objectAtIndex:section];
NSArray *sectionNames = [faves objectForKey:key];
return [sectionNames count];
}

似乎“entries”是键名 (NSString) 的 NSArray,而“faves”是部分内容 (NSArrays) 的 NSDictionary。

问题是您将键名数组 (self.entries) 替换为单个部分的内容数组。我已经扩展了您的代码以使逻辑错误更加明显:

// query for section key name by section index
NSString * keyName = [entries objectAtIndex:indexPath.section];

// retrieve array of a section's datasource (array of rows)
NSMutableArray * delArray = [faves objectForKey:keyName];

// remove row from a section's datasource
[delArray removeObjectAtIndex:indexPath.row];

// ERROR: assign an array of rows to array used for section names
self.entries = delArray;

将您的代码更改为以下内容应该会删除 UITableView 生成的断言:

(void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

//make array of the objects and remove the selected object from that array.
NSMutableArray *delArray = [faves objectForKey:[entries objectAtIndex:indexPath.section]];
[delArray removeObjectAtIndex:indexPath.row];

//write the updated entries to the plist file.
NSArray *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [rootPath objectAtIndex:0];
NSString *plistFile = [docsPath stringByAppendingPathComponent:@"Data.plist"];
[faves writeToFile:plistFile atomically:YES];

//update the actual tableview. This is where things go awry.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; //ends up trying to load the sections instead of the entries = wrong number of list items returned = crash!

//[tableView reloadData]; //works if I only use this line, but the list updates wrong, showing each entry as a section header.
}

关于iphone - 关于 UITableView 和 deleteRowsAtIndexPaths 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com