【问题标题】:Searching TableView can't select row搜索 TableView 无法选择行
【发布时间】:2016-07-29 04:22:08
【问题描述】:

在搜索 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)
    }

}

【问题讨论】:

  • 尝试在 didselectrowatindexpath 方法中关闭搜索控制器。
  • 为什么在didSelectRow中调用filterByName
  • 你有搜索栏委托功能
  • @Losiowaty filterByName 只是因为我之前尝试过的东西而留在了那里。

标签: ios swift uitableview swift2


【解决方案1】:

玩了之后才知道。显然添加以下代码可以解决问题。

searchController?.dimsBackgroundDuringPresentation = false

【讨论】:

    【解决方案2】:

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

    searchController?.obscuresBackgroundDuringPresentation = false
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多