How to deselect a selected UITableView cell?

前端 未结 24 3138
一整个雨季
一整个雨季 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:36

    It might be useful to make an extension in Swift for this.

    Swift 4 and Swift 5:

    Swift extension (e.g. in a UITableViewExtension.swift file):

    import UIKit
    
    extension UITableView {
    
        func deselectSelectedRow(animated: Bool)
        {
            if let indexPathForSelectedRow = self.indexPathForSelectedRow {
                self.deselectRow(at: indexPathForSelectedRow, animated: animated)
            }
        }
    
    }
    

    Use e.g.:

    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
    
        self.tableView.deselectSelectedRow(animated: true)
    }
    

提交回复
热议问题