Swift - 在自定义 TableViewController 中出现 SearchBar 问题

Posted

技术标签:

【中文标题】Swift - 在自定义 TableViewController 中出现 SearchBar 问题【英文标题】:Swift - Having problems with SearchBar in a custom TableViewController 【发布时间】:2017-08-04 12:15:34 【问题描述】:

我是代码新手,我想在我的自定义 TableViewController 中创建一个搜索功能。当我(在搜索栏中)键入与我创建的名称列表中的一个字母相对应的字母时,我收到一个错误(线程 1:信号 SIGABRT)。当我输入一个与我的姓名列表中的字母不对应的字母时,我没有收到错误。我检查了我的 IBoutlets,但它们不是问题。有人知道如何解决错误吗? 这是我最初的 viewcontroller 代码:(有些名字是荷兰语)

import UIKit

class elementstableviewcontroller: UITableViewController, 
UISearchResultsUpdating

@IBOutlet var tableview: UITableView!

var namen = ["Waterstof","Helium","Litium","Beryllium","Boor","Koolstof","Stikstof","Zuurstof","Fluor","Neon"]
var afkortingen = ["H","He","Li","Be","B","C","N","O","F","Ne"]
var atoommassas = ["Massa: 1,008","Massa: 4,003","Massa: 6,941","Massa: 9,012","Massa: 10,81","Massa: 12,01","Massa: 14,01","Massa: 16,00","Massa: 19,00","Massa: 20,18"]
var atoomnummers = ["Nummer: 1","Nummer: 2","Nummer: 3","Nummer: 4","Nummer: 5","Nummer: 6","Nummer: 7","Nummer: 8","Nummer: 9","Nummer: 10"]
var ladingen = ["Lading: +1,-1","Lading: 0","Lading: +1","Lading: +2","Lading: +3","Lading: +2,+4,-4","Lading: +1,+2,+3,+4,+5,-1,-2,-3","Lading: -2","Lading: -1","Lading: 0"]
var electronenconfig = ["Config: 1","Config: 2","Config: 2,1","Config: 2,2","Config: 2,3","Config: 2,4","Config: 2,5","Config: 2,6","Config: 2,7","Config: 2,8"]
var searchcontroller : UISearchController!
var resultscontroller = UITableViewController()
var filterednamen = [String]()

override func viewDidLoad() 
    super.viewDidLoad()

    self.resultscontroller.tableView.dataSource = self
    self.resultscontroller.tableView.delegate = self
    self.searchcontroller = UISearchController(searchResultsController: self.resultscontroller)
    self.tableView.tableHeaderView = self.searchcontroller.searchBar
    self.searchcontroller.searchResultsUpdater = self
    self.searchcontroller.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true

func updateSearchResults(for searchController: UISearchController) 
    self.filterednamen = self.namen.filter  (naam:String) -> Bool in
        if naam.lowercased().contains(self.searchcontroller.searchBar.text!.lowercased()) 
            return true
        else
            return false
        
    
    self.resultscontroller.tableView.reloadData()


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    if tableView == self.tableView
        return  self.namen.count
    else 
        return self.filterednamen.count
    

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
    cell.afkorting.text = afkortingen[indexPath.row]
    cell.name.text = namen[indexPath.row]
    cell.atoommassa.text = atoommassas[indexPath.row]
    cell.elektronenconfiguratie.text = electronenconfig[indexPath.row]
    cell.atoomnummer.text = atoomnummers[indexPath.row]
    cell.lading.text = ladingen[indexPath.row]


    if tableView == self.tableView
            //cell.textLabel?.text = self.namen[indexPath.row]
            cell.name.text = self.namen[indexPath.row]
    else
            //cell.textLabel?.text = self.filterednamen[indexPath.row]
            cell.name.text = self.filterednamen[indexPath.row]

    
    return cell



这是我的 CustomCell 代码:

import UIKit

class CustomCell: UITableViewCell 

@IBOutlet var elektronenconfiguratie: UILabel!
@IBOutlet var atoomnummer: UILabel!
@IBOutlet var atoommassa: UILabel!
@IBOutlet var name: UILabel!
@IBOutlet var afkorting: UILabel!
@IBOutlet var lading: UILabel!
override func awakeFromNib() 
    super.awakeFromNib()
    // Initialization code


override func setSelected(_ selected: Bool, animated: Bool) 
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state



*namen = 名字和 naam = 从荷兰语翻译成英语的名字

【问题讨论】:

【参考方案1】:

尝试在 shouldChangeTextIn SearchBar 方法中实现过滤功能,如下所示

func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 

   self.filterednamen = self.namen.filter  (naam:String) -> Bool in

return naam.lowercased().contains(self.searchcontroller.searchBar.text!.lowercased()) 
    
    self.resultscontroller.tableView.reloadData()


【讨论】:

【参考方案2】:

您的代码有几个问题。

首先,您不必要地创建了 IBOutletUITableView。从您的代码中删除它。

其次,您正在使用该表格视图来检查您是否正在搜索。这是不必要的,因为您想在同一个视图控制器中显示搜索结果,因此正确的布尔检查是:

!searchcontroller.isActive || searchcontroller.searchBar.text == ""

最后,错误的发生正是因为最后一个问题指出:您将searchResultsController 设置为幻像表视图,因此它会出现断言失败,因为它找不到标识为“单元格”的单元格。

这里是固定代码:

import UIKit

class FirstViewController: UITableViewController,
UISearchResultsUpdating

    var namen = ["Waterstof","Helium","Litium","Beryllium","Boor","Koolstof","Stikstof","Zuurstof","Fluor","Neon"]
    var afkortingen = ["H","He","Li","Be","B","C","N","O","F","Ne"]
    var atoommassas = ["Massa: 1,008","Massa: 4,003","Massa: 6,941","Massa: 9,012","Massa: 10,81","Massa: 12,01","Massa: 14,01","Massa: 16,00","Massa: 19,00","Massa: 20,18"]
    var atoomnummers = ["Nummer: 1","Nummer: 2","Nummer: 3","Nummer: 4","Nummer: 5","Nummer: 6","Nummer: 7","Nummer: 8","Nummer: 9","Nummer: 10"]
    var ladingen = ["Lading: +1,-1","Lading: 0","Lading: +1","Lading: +2","Lading: +3","Lading: +2,+4,-4","Lading: +1,+2,+3,+4,+5,-1,-2,-3","Lading: -2","Lading: -1","Lading: 0"]
    var electronenconfig = ["Config: 1","Config: 2","Config: 2,1","Config: 2,2","Config: 2,3","Config: 2,4","Config: 2,5","Config: 2,6","Config: 2,7","Config: 2,8"]
    var searchcontroller : UISearchController!
    var filterednamen = [String]()

    override func viewDidLoad() 
        super.viewDidLoad()

        self.searchcontroller = UISearchController(searchResultsController: nil)
        self.tableView.tableHeaderView = self.searchcontroller.searchBar
        self.searchcontroller.searchResultsUpdater = self
        self.searchcontroller.dimsBackgroundDuringPresentation = false
        definesPresentationContext = true
    
    func updateSearchResults(for searchController: UISearchController) 
        self.filterednamen = self.namen.filter  (naam:String) -> Bool in
            if naam.lowercased().contains(self.searchcontroller.searchBar.text!.lowercased()) 
                return true
            else
                return false
            
        

        self.tableView.reloadData()

    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        if !searchcontroller.isActive || searchcontroller.searchBar.text == "" 
            return  self.namen.count
        else 
            return self.filterednamen.count
        
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
        cell.afkorting.text = afkortingen[indexPath.row]
        cell.name.text = namen[indexPath.row]
        cell.atoommassa.text = atoommassas[indexPath.row]
        cell.elektronenconfiguratie.text = electronenconfig[indexPath.row]
        cell.atoomnummer.text = atoomnummers[indexPath.row]
        cell.lading.text = ladingen[indexPath.row]


        if !searchcontroller.isActive || searchcontroller.searchBar.text == "" 
            //cell.textLabel?.text = self.namen[indexPath.row]
            cell.name.text = self.namen[indexPath.row]
        else
            //cell.textLabel?.text = self.filterednamen[indexPath.row]
            cell.name.text = self.filterednamen[indexPath.row]

        
        return cell
    


【讨论】:

以上是关于Swift - 在自定义 TableViewController 中出现 SearchBar 问题的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 如何在自定义 AlertController 中点击按钮时呈现 ViewController

Swift:在自定义视图中未调用 UIButton 触摸事件

在自定义单元格 Swift 中保存 uibutton 状态

UIWebViewDelegate DidFinishLoad 未在自定义类中触发 (Swift)

Ios Swift:在自定义标签栏中显示导航栏

以swift语言在自定义表格视图单元格中更新进度视图下载