同时插入行和删除行。 UITableView的 [英] Inserting row and deleting row simultaneously. UITableView

查看:159
本文介绍了同时插入行和删除行。 UITableView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have a UITableView that represents a video library. The datasource of the library is a NSMutableDictionary that contains a NSMutableArray for each of its keys. Each of the Arrays contains an Array with all the info for each value.

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

NSMutableDictionary -> Foreach Key a NSMutableArray that contains -> NSMutableArrays.

tableSectionData 是我的数据源。

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

I have been trying to insert a row on the first section and delete another row in another section. This is the code I am using:

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

EDIT This is the new attempt. I first update the datasource and then add and delete the corresponding rows that are indexed with my 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];

我认为这样可行,因为如果我想插入一行(我必须先删除一行)我希望两种情况都这样做。)

I thought this would work since I have to first delete a row if I want to insert one (in the case I want to do both.)

具体错误如下:

'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).'

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

I know what the error says, that I don't have a corresponding number of rows on the section however I don't see my mistake on the code and I logged the datasource and it corresponds to the desired result.

非常感谢任何帮助和建议!

Any help and suggestions would be greatly appreciated!

推荐答案

你必须在开始/结束更新方法内进行所有更新。如果你有多个UITableView更新,它总是有效。

You have to do all updates inside begin/end updates methods. It's valid always if you have more than one UITableView update.

并且在调用endUpdates之前不要忘记更新dataSource!

And don't forget update dataSource before endUpdates called!

[self.tableView beginUpdates];

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

[self.tableView endUpdates];

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

your mistake here - is you are using different begin/end updates sections in your code sample

这篇关于同时插入行和删除行。 UITableView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆