Why does UITableViewCell remain highlighted?

前端 未结 18 1260
余生分开走
余生分开走 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 13:12

    Did you subclass -(void)viewWillAppear:(BOOL)animated? The selected UITableViewCell won't deselect when you don't call [super viewWillAppear:animated]; in your custom method.

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

    I was getting this problem as well for my drill-down application. After a viewcontroller, which I'll call VC, returns after pushing another ViewController, the selected cell in VC remained highlighted. In my app, I had created VC to handle the second level (out of three levels) of my drill-down.

    The problem in my case is that VC was a UIViewController (that contained a View that contained a TableView). I instead made VC a UITableViewController (that contained a TableView). The UITableViewController class automatically handles the de-highlighting of the table cell after returning from a push. The second answer to the post "Issue with deselectRowAtIndexPath in tableView" gives a more complete answer to this problem.

    The problem did not occur for the root viewcontroller because when I created the app as a "Navigation-based App" in XCode, the resulting root viewcontroller was already made to subclass UITableViewController.

    0 讨论(0)
  • 2020-12-12 13:15
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    }
    
    0 讨论(0)
  • 2020-12-12 13:17

    I am using CoreData so the code that worked for me was a combination of ideas from various answers, in Swift:

    override func viewDidAppear(_ animated: Bool) {
        if let testSelected = yourTable.indexPathForSelectedRow {
            yourTable.deselectRow(at: testSelected, animated: true)
        }
        super.viewDidAppear(true)
    }
    
    0 讨论(0)
  • 2020-12-12 13:17

    I've been having the same issue for long time so in case anyone else is struggling:

    Take a look at your -tableView: cellForRowAtIndexPath: and see if you are creating cells or using a 'reuse identifier'. If the latter, make sure that your table in IB has a cell with that identifier. If you're not using a reuse Identifier just create a new cell for each row.

    This should then give your table the expected 'fade selected row' on appearing.

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

    Swift 5 Solution:

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)
    }
    
    0 讨论(0)
提交回复
热议问题