Why does UITableViewCell remain highlighted?

前端 未结 18 1258
余生分开走
余生分开走 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条回答
  • 2020-12-12 12:55

    If none of these work for you, consider this work-around:

    Use an unwind segue to call:

    @IBAction func unwind_ToTableVC (segue: UIStoryboardSegue) {
        if let index = tableView.indexPathForSelectedRow {
            tableView.deselectRowAtIndexPath(index, animated: true)
        }
    }
    

    Why do this? Primarily if you're having trouble getting the deselect code to run at the right time. I had trouble with it not working on the viewWillAppear so the unwind worked a lot better.

    Steps:

    1. Write the unwind segue (or paste from above) into your 1st VC (the one with the table)

    2. Go to the 2nd VC. Control-drag from the Cancel/Done/Etc button you're using to dismiss that VC and drag to the Exit Icon at the top.

    3. Select the unwind segue you created in step 1

      Good luck.

    0 讨论(0)
  • 2020-12-12 12:56

    In your didSelectRowAtIndexPath you need to call deselectRowAtIndexPath to deselect the cell.

    So whatever else you are doing in didSelectRowAtIndexPath you just have it call deselectRowAtIndexPath as well.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     // Do some stuff when the row is selected
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    0 讨论(0)
  • 2020-12-12 12:57

    For the Swift users, add this to your code:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    

    It's paulthenerd's answer except in Swift instead of Obj-C.

    0 讨论(0)
  • 2020-12-12 12:57

    Updated with Swift 4

    After few experiments, also based of previous answers, I've got the conclusion that the best behaviour can be achieved in 2 ways: (almost identical in practice)

    // First Solution: delegate of the table View
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
    }
    
    // Second Solution: With the life cycle of the view.
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    
        let selectedRow: IndexPath? = tableView.indexPathForSelectedRow
        if let selectedRow = selectedRow {
            tableView.deselectRow(at: selectedRow, animated: false)
        }
    }
    

    I'm personally adopting the first solution, because it's simply more concise. Another possibility, if you need a little animation when you return to your tableView, is to use viewWillAppear:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        let selectedRow: IndexPath? = _view.tableView.indexPathForSelectedRow
        if let selectedRow = selectedRow {
            _view.tableView.deselectRow(at: selectedRow, animated: true)
        }
    }
    

    Last but not least, if you're using a UITableViewController, you can also take advantage of the property clearsSelectionOnViewWillAppear.

    0 讨论(0)
  • 2020-12-12 13:01

    Xcode 10, Swift 4

    I had this same issue and discovered I left an empty call to viewWillAppear at the bottom of my tableViewController. Once I removed the empty override function the row no longer stayed highlighted upon return to the tableView view.

    problem func

    override func viewWillAppear(_ animated: Bool) {
      // need to remove this function if not being used.
    }
    

    removing empty function solved my problem.

    0 讨论(0)
  • 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)
        }
    }
    
    0 讨论(0)
提交回复
热议问题