gpt4 book ai didi

ios - 从 TableView 中删除行

转载 作者:行者123 更新时间:2023-11-29 01:54:25 25 4
gpt4 key购买 nike

我正在开发一个 IOS 应用程序。删除 TableView 中的行并加载 TableView 中核心数据中的数据。当单击删除按钮应用程序崩溃。 原因就是原因:无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (18) 必须等于更新前该节中包含的行数 (18) ),加上或减去从该部分插入或删除的行数(0 插入,1 删除)以及加上或减去移入或移出该部分的行数(0 移入,0 移出)...

_fetchedobj 是一个 NSArray,用于从核心数据中获取数据

//Code is
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return _fetchedobj.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyReuseIdentifier";
UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
data = [_fetchedobj objectAtIndex:indexPath.row];
return cell;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
NSManagedObject *managedObject = [_fetchedobj objectAtIndex:indexPath.row];
[self.managedObjectContext deleteObject:managedObject];
[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myTable endUpdates];
}
}

最佳答案

难道你忘了从你的数据源数组中删除这个对象吗?

尝试重新填充您的 _fetchedobj,因为您必须在删除后配置您的模型。有关详细信息,请阅读 https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html

试试这个

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
NSManagedObject *managedObject = [_fetchedobj objectAtIndex:indexPath.row];
[self.managedObjectContext deleteObject:managedObject];
// insert this line to your code

NSMutableArray *newA = [_fetchedobj mutableCopy ];
[newA removeObjectAtIndex: indexPath.row];
_fetchedobj = newA;

[myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[myTable endUpdates];
}
}

关于ios - 从 TableView 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31048770/

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