gpt4 book ai didi

ios - 移动行在索引路径 : how to delete section once last row is moved to another section

转载 作者:可可西里 更新时间:2023-11-01 05:48:27 28 4
gpt4 key购买 nike

我有一个包含多个部分的表格 View 。我希望能够将行从一个部分移动到另一个部分,并在没有行时删除一个部分。我正在尝试通过 moveRowAtIndexPath 执行此操作,但我的代码不起作用并抛出 NSRangeException 异常。

这是一个代码示例:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

NSUInteger fromSection = [fromIndexPath section];
NSUInteger fromRow = [fromIndexPath row];
NSString *fromKey = [self.keys objectAtIndex:fromSection];
NSMutableArray *fromEventSection = [self.eventsDict objectForKey:fromKey];

NSUInteger toSection = [toIndexPath section];
NSUInteger toRow = [toIndexPath row];
NSString *toKey = [self.keys objectAtIndex:toSection];
NSMutableArray *toEventSection = [self.eventsDict objectForKey:toKey];

id object = [[fromEventSection objectAtIndex:fromRow] retain];
[fromEventSection removeObjectAtIndex:fromRow];
[toEventSection insertObject:object atIndex:toRow];
[object release];
// The above code works just fine!

// Try to delete an empty section. Here is where trouble begins:
if ((fromSection != toSection) && [fromEventSection count] == 0) {
[self.keys removeObjectAtIndex:fromSection];
[self.eventsDict removeObjectForKey:fromKey];

[tableView deleteSections:[NSIndexSet indexSetWithIndex:fromSection] withRowAnimation:UITableViewRowAnimationFade];
}

最佳答案

我有幸在 moveRowAtIndexPath 方法结束后的一个 block 中执行 deleteSections 方法,方法是将删除包装在 dispatch_async 中。

    dispatch_async(dispatch_get_main_queue(), ^{
if ((fromSection != toSection) && [fromEventSection count] == 0) {
[self.keys removeObjectAtIndex:fromSection];
[self.eventsDict removeObjectForKey:fromKey];
[tableView deleteSections:[NSIndexSet indexSetWithIndex:fromSection] withRowAnimation:UITableViewRowAnimationFade];
}
});

关于ios - 移动行在索引路径 : how to delete section once last row is moved to another section,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1549632/

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