Why does UITableViewCell remain highlighted?

前端 未结 18 1261
余生分开走
余生分开走 2020-12-12 12:45

What would cause a table view cell to remain highlighted after being touched? I click the cell and can see it stays highlighted as a detail view is pushed. Once the detail

18条回答
  •  -上瘾入骨i
    2020-12-12 13:02

    The most clean way to do it is on viewWillAppear:

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        // Unselect the selected row if any
        NSIndexPath*    selection = [self.tableView indexPathForSelectedRow];
        if (selection) {
            [self.tableView deselectRowAtIndexPath:selection animated:YES];
        }
    }
    

    This way you have the animation of fading out the selection when you return to the controller, as it should be.

    Taken from http://forums.macrumors.com/showthread.php?t=577677

    Swift version

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        // deselect the selected row if any
        let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
        if let selectedRowNotNill = selectedRow {
            tableView.deselectRow(at: selectedRowNotNill, animated: true)
        }
    }
    

提交回复
热议问题