How to deselect a selected UITableView cell?

前端 未结 24 3123
一整个雨季
一整个雨季 2020-12-07 10:10

I am working on a project on which I have to preselect a particular cell.

I can preselect a cell using -willDisplayCell, but I can\'t deselect it when t

相关标签:
24条回答
  • 2020-12-07 10:23

    Swift 4:

    tableView.deselectRow(at: indexPath, animated: true)
    
    0 讨论(0)
  • 2020-12-07 10:23

    Swift 3

    I've run into this as well - when you navigate or unwind back to the table view it usually doesn't deselect the previously selected row for you. I put this code in the table view controller & it works well:

    override func viewDidAppear(_ animated: Bool) {
        if let lastSelectedRow = tableView.indexPathForSelectedRow {
            tableView.deselectRow(at: lastSelectedRow, animated: true)
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:24
    NSIndexPath * index = [self.menuTableView indexPathForSelectedRow];
    [self.menuTableView deselectRowAtIndexPath:index animated:NO];
    

    place this code according to your code and you will get your cell deselected.

    0 讨论(0)
  • 2020-12-07 10:25

    Swift 3.0:

    Following the protocol conformance section of the ray wenderlich swift style guide, to keep related methods grouped together, put this extension below your view controller class like that:

    // your view controller
    class MyViewcontroller: UIViewController {
      // class stuff here
    }
    
    // MARK: - UITableViewDelegate
    extension MyViewcontroller: UITableViewDelegate {
          // use the UITableViewDelegate method tableView(_:didSelectRowAt:)
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            // your code when user select row at indexPath
    
            // at the end use deselectRow
            tableView.deselectRow(at: indexPath, animated: true)
        }
    }
    
    0 讨论(0)
  • 2020-12-07 10:26

    try this

    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
    
    0 讨论(0)
  • 2020-12-07 10:30

    This should work:

    [tableView deselectRowAtIndexPath:indexpath1 animated:NO];
    

    Just make sure materialTable and tableView are pointing to the same object.

    Is materials connected to the tableView in Interface Builder?

    0 讨论(0)
提交回复
热议问题