Joz*_*oze 9 iphone objective-c uitableview ios
所以我有一个UITableView代表视频库的代码.库的数据源是一个NSMutableDictionary包含NSMutableArray每个键的数据源.每个数组都包含一个数组,其中包含每个值的所有信息.
NSMutableDictionary- > Foreach Key a NSMutableArray包含 - > NSMutableArrays.
tableSectionData是我的数据源.
我一直试图在第一部分插入一行,并删除另一部分中的另一行.这是我正在使用的代码:
编辑这是新的尝试.我首先更新数据源,然后添加和删除使用我的dataSource索引的相应行.
[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];
Run Code Online (Sandbox Code Playgroud)
我认为这样可行,因为如果我想插入一行,我必须首先删除一行(在我想要两者的情况下).
具体错误如下:
'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).'
Run Code Online (Sandbox Code Playgroud)
我知道错误说的是,我在该部分没有相应的行数但是我没有看到我在代码上的错误并记录了数据源并且它对应于所需的结果.
任何帮助和建议将不胜感激!
iiF*_*man 11
您必须在开始/结束更新方法中进行所有更新.如果您有多个UITableView更新,它始终有效.
并且在调用endUpdates之前不要忘记更新dataSource!
[self.tableView beginUpdates];
// do your staff here, like:
[self.tableView inserRowsAtIndexPaths:indexPathsToInsert];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
你的错误 - 你在代码示例中使用了不同的开始/结束更新部分
| 归档时间: |
|
| 查看次数: |
5888 次 |
| 最近记录: |