swift2 搜索TableView时无法选择行

s71maibg  于 2022-11-23  发布在  Swift
关注(0)|答案(3)|浏览(280)

在搜索tableView时,每次我试图选择一行时,它都会将我带回到未搜索的tableView。我错过了什么?在不对表进行筛选时,segue可以完美地工作。在激活searchBar时,选择行的功能会消失。

import UIKit
import Foundation

class BenchmarkWODViewController: UITableViewController, UISearchResultsUpdating {

    var WodList = [WOD]()
    var FilteredWodList = [WOD]()
    var Keyword = ""
    var searchController : UISearchController?
    var index = Int()

    @IBAction func backButton(sender: AnyObject) {
        self.navigationController?.popViewControllerAnimated(true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        for wodData in BenchmarkWODs.library {
            let wod = WOD(dictionary: wodData)
            WodList.append(wod)
    }

    // Search Bar
        self.searchController = UISearchController(searchResultsController: nil)
        self.searchController?.searchBar.autocapitalizationType = .None
        self.tableView.tableHeaderView = self.searchController?.searchBar
        self.searchController?.searchResultsUpdater = self
        self.Keyword = ""
        definesPresentationContext = true

        self.filterByName()
    }

    func filterByName(){
        self.FilteredWodList = self.WodList.filter({ (wod: WOD) -> Bool in
            if self.Keyword.characters.count == 0 {
            return true
        }

        if (wod.name?.lowercaseString.rangeOfString(self.Keyword.lowercaseString)) != nil {
            return true
        }
        return false
    })
        self.tableView.reloadData()
    }

    // Search Bar Function
    func updateSearchResultsForSearchController(searchController:     UISearchController) {
        Keyword = searchController.searchBar.text!
        self.filterByName()
    }


    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.FilteredWodList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("BenchmarkCell", forIndexPath: indexPath) as UITableViewCell
        let wod = self.FilteredWodList[indexPath.row]

        if let wodName = wod.name {
        cell.textLabel?.text = wodName
    }
        return cell
    }


    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.filterByName()
        self.performSegueWithIdentifier("showBenchmarkDetail", sender: nil)
    }

}
7tofc5zh

7tofc5zh1#

在玩了一圈之后就弄明白了。显然添加下面的代码纠正了这个问题。

searchController?.dimsBackgroundDuringPresentation = false
j2datikz

j2datikz2#

swift 'dimsBackgroundDuringPresentation'在iOS 12.0中已弃用请改用obscuresBackgroundDuringPresentation属性。

searchController?.obscuresBackgroundDuringPresentation = false
ssm49v7z

ssm49v7z3#

searchController.obscureBAckgroundDuringPresentation = false在IOS 12.0中被弃用,所以对我来说,这是添加到tableview的其他手势检测器的问题,因此请确保您没有任何其他手势检测器和touchesview方法,它们会扭曲tableview的委托方法(didSelectAtRow)的正常工作流,希望它能正常工作,

相关问题