Ios UITableView的IndExpathForSelectedRows:在插入新行时不会更新
我有一个UITableView,它可以周期性地将用户的新行置于顶部,并支持多种选择 问题是indexPathsForSelectedRows:在下一行插入时未正确更新 我认为它根本不会得到更新,并试图找到一种更好的方法来跟踪选定的单元格,但它会在随后的行插入中得到更新 我添加了一些日志语句来测试它:Ios UITableView的IndExpathForSelectedRows:在插入新行时不会更新,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个UITableView,它可以周期性地将用户的新行置于顶部,并支持多种选择 问题是indexPathsForSelectedRows:在下一行插入时未正确更新 我认为它根本不会得到更新,并试图找到一种更好的方法来跟踪选定的单元格,但它会在随后的行插入中得到更新 我添加了一些日志语句来测试它: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSL
- (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}"
)
已完成数据源中选定对象的删除。除了在问题中发布的两种方法之外,您还将indexPathsForSelectedRows记录在哪里?更新了问题。这可能有助于显示添加其他行的代码。日志输出似乎表明正在添加的第二行实际上是在第一行之后添加的,然后在第一行之前添加第三行和第四行。在插入行时清除了问题并添加了日志消息。新行总是在path=0-0处插入。