gpt4 book ai didi

objective-c - 删除 UITableView 中的行时出错

转载 作者:行者123 更新时间:2023-12-01 19:23:45 25 4
gpt4 key购买 nike

尝试从 UITableView 中删除一行时出现此错误。如果我删除 tableview 中的最后一行,则不会出错并且一切正常,但任何其他行都会引发异常。有人可以告诉我我在这里做错了什么吗?任何帮助将非常感激!

错误:

'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'



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

//1. clear existing URL's
NSURL *urlToDelete = nil;

//2. If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];
//[documentURLs removeObjectAtIndex:row];
}
//3. update the tableview on the fly without reloading the data
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];

//4. get the location of the files
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocumentsDirectory = [paths objectAtIndex:0];

//5.setup file manager
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:pathToDocumentsDirectory];
NSLog(@"Path to file: %@", pathToDocumentsDirectory);
NSLog(@"File exists: %d", fileExists);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:pathToDocumentsDirectory]);

//6. remove if matches
if (urlToDelete) {
BOOL success = [fileManager removeItemAtURL:urlToDelete error:&error];
if (!success) NSLog(@"Error: %@", [error localizedDescription]);
}
}

最佳答案

您必须分三个阶段更新表。

1)处理你的模型

[documentURLs removeObjectAtIndex:row];

2)处理你的 table 的动画
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

3)处理您的文件更新(也许之前可以完成,因为您可能在文件删除时出错)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

NSURL *urlToDelete = nil;

if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];
[documentURLs removeObjectAtIndex:row]; // you need to update your model

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

// do stuff to update the file here
}
}

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

NSURL *urlToDelete = nil;

if(editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
urlToDelete = [documentURLs objectAtIndex:row];

// do stuff to update the file here...

if(success)
{
[documentURLs removeObjectAtIndex:row]; // you need to update your model

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}

笔记。如果从模型中删除项目并且模型计数等于 0,则必须注意。在这种情况下,您必须删除整个部分。

希望能帮助到你。

附言检查代码,因为我在没有 XCode 的情况下编写

关于objective-c - 删除 UITableView 中的行时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8954050/

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