gpt4 book ai didi

iphone - 同时插入行和删除行。 UI表格 View

转载 作者:技术小花猫 更新时间:2023-10-29 11:18:57 24 4
gpt4 key购买 nike

所以我有一个代表视频库的 UITableView。该库的数据源是一个 NSMutableDictionary,其中每个键都包含一个 NSMutableArray。每个数组都包含一个数组,其中包含每个值的所有信息。

NSMutableDictionary -> Foreach Key a NSMutableArray 包含 -> NSMutableArrays

tableSectionData是我的数据源。

我一直在尝试在第一部分插入一行并在另一部分删除另一行。这是我正在使用的代码:

EDIT 这是新的尝试。我首先更新数据源,然后添加和删除用我的数据源索引的相应行。

[mainTableView beginUpdates];
NSMutableDictionary *clone = [[NSMutableDictionary alloc]
initWithDictionary:self.tableSectionData copyItems:YES];
for (NSString *key in [clone allKeys]) {
if([key isEqualToString:@"Videos available for download"]) {
for (NSArray *videoArray in [clone objectForKey:key]) {
if ([[videoArray objectAtIndex:0] isEqualToString:helper.videoName]) {
[[self.tableSectionData objectForKey:@"Downloaded videos"]
addObject:videoArray];
NSUInteger insertIndex = [[self.tableSectionData
objectForKey:@"Downloaded videos"]
indexOfObject:videoArray];
NSIndexPath *pathToInsert = [NSIndexPath
indexPathForRow:insertIndex inSection:0];
NSArray *indexesToAddition = [NSArray
arrayWithObject:pathToInsert];
[mainTableView insertRowsAtIndexPaths:indexesToAddition
withRowAnimation:UITableViewRowAnimationFade];
[[self.tableSectionData
objectForKey:@"Videos available for download"] removeObject:videoArray];
NSUInteger deleteIndex = [[self.tableSectionData
objectForKey:@"Videos available for download"] indexOfObject:videoArray];
NSIndexPath *pathToDelete = [NSIndexPath
indexPathForRow:deleteIndex inSection:1];
NSArray *indexesToDeletion = [NSArray arrayWithObject:
pathToDelete];
[mainTableView deleteRowsAtIndexPaths:indexesToDeletion
withRowAnimation:UITableViewRowAnimationFade];

}
}
}
}
[mainTableView endUpdates];

我认为这会起作用,因为如果我想插入一行,我必须先删除一行(如果我想同时插入一行。)

具体报错如下:

'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (3), 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).'

我知道错误是什么意思,我在该部分没有相应的行数,但是我没有在代码中看到我的错误,我记录了数据源,它对应于所需的结果。

如有任何帮助和建议,我们将不胜感激!

最佳答案

您必须在开始/结束更新方法中执行所有更新。如果您有多个 UITableView 更新,它始终有效。

并且不要忘记在调用 endUpdates 之前更新数据源!

[self.tableView beginUpdates];

// do your staff here, like:
[self.tableView inserRowsAtIndexPaths:indexPathsToInsert];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete];

[self.tableView endUpdates];

您的错误 - 您是否在代码示例中使用了不同的开始/结束更新部分

关于iphone - 同时插入行和删除行。 UI表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14958020/

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