UITableView的indexPathsForSelectedRows:插入新行时不会更新

时间:2014-04-11 03:21:01

标签: ios objective-c uitableview

我有UITableView定期获取新行,并支持多种选择。

问题是indexPathsForSelectedRows:未在下一行插入中得到正确更新。

我认为它根本没有得到更新,并试图找到一种更好的方法来跟踪选定的单元格,但它会在后续的行插入中得到更新。

我添加了一些日志语句来测试它:

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection: %@", @(_messages.count));
    return _messages.count;
}

- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"willSelectRowAtIndexPath: %@ indexPathsForSelectedRows: %@", indexPath, tableView.indexPathsForSelectedRows);
    return indexPath;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath: %@ indexPathsForSelectedRows: %@", indexPath, tableView.indexPathsForSelectedRows);
}

此外,我在返回应选择的单元格的高度时记录indexPathsForSelectedRows

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Selected cell?
    if ([tableView.indexPathsForSelectedRows containsObject:indexPath])
    {
        NSLogInfo(@"indexPathsForSelectedRows: %@", tableView.indexPathsForSelectedRows);
    }

    // ...
}

添加/删除行的相关代码:

- (void)updateTableViewRowsRemoving:(NSInteger)itemsToRemoveCount
                          inserting:(NSInteger)itemsToInsertCount
{
    // Code to calculate removePaths and insertPaths
    // ...

    [self.tableView beginUpdates];
    if (itemsToRemoveCount > 0)
    {
        [self.tableView deleteRowsAtIndexPaths:removePaths
                              withRowAnimation:UITableViewRowAnimationFade];
        NSLogVerbose(@"deleteRowsAtIndexPaths: %@", removePaths);
    }
    [self.tableView insertRowsAtIndexPaths:insertPaths
                          withRowAnimation:UITableViewRowAnimationFade];
    NSLogVerbose(@"insertRowsAtIndexPaths: %@", insertPaths);
    [self.tableView endUpdates];
}

我让第一个出现两行的另一个测试,然后选择最后一行(1)。顶部会添加一个新行,所选行仍然是1,并且只有在它开始更新后才会显示。

2014-04-11 14:28:55.653 ConsoleDemo[77409:60b] numberOfRowsInSection: 0
2014-04-11 14:29:01.939 ConsoleDemo[77409:60b] numberOfRowsInSection: 1
2014-04-11 14:29:09.241 ConsoleDemo[77409:60b] insertRowsAtIndexPaths: (
    "<NSIndexPath: 0x8d7e610> {length = 2, path = 0 - 0}"
)
2014-04-11 14:29:09.242 ConsoleDemo[77409:60b] numberOfRowsInSection: 2
2014-04-11 14:29:10.584 ConsoleDemo[77409:60b] willSelectRowAtIndexPath: <NSIndexPath: 0x8f82820> {length = 2, path = 0 - 1} indexPathsForSelectedRows: (null)
2014-04-11 14:29:10.584 ConsoleDemo[77409:60b] didSelectRowAtIndexPath: <NSIndexPath: 0x8f82820> {length = 2, path = 0 - 1} indexPathsForSelectedRows: (
    "<NSIndexPath: 0x8f81440> {length = 2, path = 0 - 1}"
)
2014-04-11 14:29:14.741 ConsoleDemo[77409:60b] insertRowsAtIndexPaths: (
    "<NSIndexPath: 0x8c774e0> {length = 2, path = 0 - 0}"
)
2014-04-11 14:29:14.742 ConsoleDemo[77409:60b] numberOfRowsInSection: 3
2014-04-11 14:29:14.742 ConsoleDemo[77409:60b] indexPathsForSelectedRows: (
    "<NSIndexPath: 0x8f81440> {length = 2, path = 0 - 1}"
)
2014-04-11 14:29:20.443 ConsoleDemo[77409:60b] insertRowsAtIndexPaths: (
    "<NSIndexPath: 0x8e7fe60> {length = 2, path = 0 - 0}"
)
2014-04-11 14:29:20.444 ConsoleDemo[77409:60b] numberOfRowsInSection: 4
2014-04-11 14:29:20.444 ConsoleDemo[77409:60b] indexPathsForSelectedRows: (
    "<NSIndexPath: 0x8ccd300> {length = 2, path = 0 - 2}"
)

1 个答案:

答案 0 :(得分:0)

完成dataSource中所选对象的manually keeping track