为什么不能同时调用viewWillAppear:animated中的reloadData和deselectRowAtIndexPath动画? [英] Why can't I call reloadData AND deselectRowAtIndexPath in viewWillAppear:animated at the same time?

查看:81
本文介绍了为什么不能同时调用viewWillAppear:animated中的reloadData和deselectRowAtIndexPath动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有单元格的UITableView,当选定单元格时会将viewControllers推入堆栈。子viewController接受用户输入,然后弹出堆栈。

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中:动画-如果将deselectRowAtIndexPath放在viewWillAppear中并将reloadData放在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.

推荐答案

重新加载单元格时,它会自动取消选择。那是因为您没有在 tableView:cellForRowAtIndexPath中将单元格的 selected 属性设置为 YES 。因此,您将不得不以不同的方式来处理。要么确定 indexPath 中的单元格需要选择,然后适当地设置其选择属性设置为 tableView:cellForRowAtIndexPath:中的 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];

步骤顺序为:-


  1. 重新加载没有动画的行。

  2. 选择没有动画的行。

  3. 取消选择有动画的行。

这样,我认为您可以获得想要的效果。

This way I think you can get the effect you want.

这篇关于为什么不能同时调用viewWillAppear:animated中的reloadData和deselectRowAtIndexPath动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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