Iphone 同时插入行和删除行。UITableView
因此,我有一个表示视频库的Iphone 同时插入行和删除行。UITableView,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,因此,我有一个表示视频库的UITableView。库的数据源是一个NSMutableDictionary,它包含一个NSMutableArray,用于每个键。每个数组都包含一个数组,其中包含每个值的所有信息 NSMutableDictionary->每个键都包含一个NSMutableArray的NSMutableArray tableSectionData是我的数据源 我一直在尝试在第一节中插入一行,并在另一节中删除另一行。这是我正在使用的代码: 编辑这是新的尝试。首先更新数据源,然后添加和删除
UITableView
。库的数据源是一个NSMutableDictionary
,它包含一个NSMutableArray
,用于每个键。每个数组都包含一个数组,其中包含每个值的所有信息
NSMutableDictionary
->每个键都包含一个NSMutableArray
的NSMutableArray
tableSectionData
是我的数据源
我一直在尝试在第一节中插入一行,并在另一节中删除另一行。这是我正在使用的代码:
编辑这是新的尝试。首先更新数据源,然后添加和删除使用数据源编制索引的对应行
[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,并在numberOfRowsInSection中返回准确的行数…您必须在begin/end Update方法中执行所有更新。如果有多个UITableView更新,则它始终有效 在调用endUpdates之前不要忘记更新数据源
[self.tableView beginUpdates];
// do your staff here, like:
[self.tableView inserRowsAtIndexPaths:indexPathsToInsert];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete];
[self.tableView endUpdates];
这里的错误是您在代码示例中使用了不同的开始/结束更新部分您的数据源在哪里 删除前
[dataSource removeObjectAtIndex:nIndexDelete];
[mainTableView deleteRowsAtIndexPaths:indexesToDeletion
withRowAnimation:UITableViewRowAnimationFade];
添加之前
[dataSource inserObject:object atIndex:nIndexAdd];
[mainTableView insertRowsAtIndexPaths:indexesToAddition
withRowAnimation:UITableViewRowAnimationFade];
我已经试过了,但没用。我将坚持开始和结束更新!对于代码的注释,我已经声明我已经尝试过了,这就是为什么我尝试在单独的部分中使用它。所以这不是问题所在。另外,insertRowsAtIndexPaths必须在deleteRowsAtIndexPaths之后,否则根据文档和我已经进行的测试,它将无法工作。无论如何,谢谢你的建议。嘿,谢谢,我已经做了(或者很可能我做错了)看看第一个,我在哪里迭代我的数据源的克隆。我的数据源是tableSectionData,在那里我添加和删除有问题的对象。我要做的是将一个部分的一行转移到另一个部分。我看不出我的代码中有错误……我可以知道您在方法NumberOfSectionsTableView:和tableView:numberOfRowsInSection:中使用了什么代码吗?我知道UITableViewDelegate的这些部分是正确的,这更多的是调用更新的顺序问题。reloadData方法对我有效,但我仍然不明白为什么插入和删除不起作用。看看更新后的问题。为什么不将所有要删除的索引放在一个indexToDeleteArray中,并将索引插入indexToInsertArray中?我认为在删除行和插入行之间循环不是一个好主意。顺便说一句,还要跟踪insertIndex和deleteIndex,以查看它们是否是要删除的索引