21

我有一个 UITableView,在 Storyboard 中有一个单元格,还有一个将单元格连接到另一个视图的 segue。

当您选择一个单元格时,它会显示单元格选择动画(在我的情况下将单元格变为灰色)并将另一个视图推到屏幕上。但是当您返回 tableview 时,取消选择动画根本不显示(选择动画的反面)。由于我只是使用 segue,因此我希望默认情况下会处理此问题。

有没有办法强制它显示取消选择动画?

4

6 回答 6

26

UITableViewController如果您的视图控制器是的子类并clearsSelectedOnViewWillAppear设置为YES(这是默认值),这将自动处理。

在您的情况下,您可以以与执行此操作相同的方式执行此UITableViewController操作。取消选择 中的选定行-viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
于 2011-12-16T07:52:05.507 回答
11

确保您正在调用和方法的super实现viewWill...viewDid...

于 2011-12-27T04:00:13.113 回答
11

不确定 segues 的使用,但我经常想在视图控制器出现时刷新数据。但是,如果重新加载表,则会清除选择行。这是我用来维护选定行并在返回时显示取消选择动画的一些代码。这可能会对您有所帮助,因此我将在此处发布。

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    [tableView reloadData];
    if(indexPath) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    if(indexPath) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];  
    }
}
于 2011-12-28T08:30:33.320 回答
5

对于斯威夫特 3

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)
    if let path = tableView.indexPathForSelectedRow {

        tableView.deselectRow(at: path, animated: true)
    }
}
于 2017-05-18T03:33:43.523 回答
1

更好的 Swift 更新:

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if let path = tableView.indexPathForSelectedRow {

        tableView.deselectRowAtIndexPath(path, animated: true)
    }
}
于 2016-02-02T19:35:41.897 回答
1

快速更新:-

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    if tableView.indexPathForSelectedRow != nil {
        let indexPath: NSIndexPath = tableView.indexPathForSelectedRow!
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}
于 2015-10-12T09:27:30.593 回答