为什么我不能在viewWillAppear:animated同时调用reloadData和deselectRowAtIndexPath? [英] Why can't I call reloadData AND deselectRowAtIndexPath in viewWillAppear:animated at the same time?
问题描述
我有一个UITableView与单元格,推动viewControllers到堆栈时选择。子viewControllers接受用户输入,然后弹出堆栈。
I have a UITableView with cells that push viewControllers onto the stack when selected. The child viewControllers take user input and then pops off the stack.
当子viewController弹出时,我想让父tableView更新所选单元格的值,然后取消选择该行。我可以使用reloadData更新单元格,我可以使用deselectRowAtIndexPath - 取消选择行,但我不能同时进行。
When the child viewController is popped, I want the parent tableView to update the value of the selected cell AND then deselect the row. I can update the cell using reloadData, and I can deselect the row using deselectRowAtIndexPath - but I can't do both at the same time.
我明白为什么这是 - reloadData隐式取消选择单元格,并且deselectRowAtIndexPath显式地取消选择它,但我发现很奇怪,我找不到任何人想要实现相同重新载入/取消选择行为。
I understand why this is - reloadData deselects the cell implicitly, and deselectRowAtIndexPath deselects it explicitly, but I find it curious that I can't find anyone wanting to achieve the same reload/deselect behavior. What am I missing here?
所有的代码都在viewWillAppear:animated - 如果我把deselectRowAtIndexPath放在viewWillAppear和reloadData in viewDidAppear,我可以关闭,但这不是什么
All code is in viewWillAppear:animated - I can get close if I put deselectRowAtIndexPath in viewWillAppear and reloadData in viewDidAppear, but this isn't what I want.
推荐答案
重新载入储存格时,系统会自动取消选取。这是因为你不在中将单元格的
属性设置为
。所以你将不得不处理这个不同。要么识别 YES
tableView:cellForRowAtIndexPath: indexPath
的单元需要选择
并适当地设置其 selected < $
在
中的
属性到
YES
或者在重新加载数据后选择它。在这种情况下,您可以执行以下方法 -
When you reload a cell, it automatically gets deselected. That's because you don't set a cell's selected
property to YES
in tableView:cellForRowAtIndexPath:
. So you will have to deal with this differently. Either identify that the cell at indexPath
needs to be selected
and appropriately set its selected
property to YES
in tableView:cellForRowAtIndexPath:
or select it after you reload the data. In such case, you can execute the following methods –
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationNone];
[self.tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
步骤的顺序是: -
The order of steps are :-
- 重新载入没有任何动画的行。
- 选择没有动画的行。 / li>
- Reload the row without any animation.
- Select the row without animation.
- Deselect the row with animation.
这种方式我想你可以得到你想要的效果。
This way I think you can get the effect you want.
这篇关于为什么我不能在viewWillAppear:animated同时调用reloadData和deselectRowAtIndexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!