You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UITableView中添加UISearchBar输入文本无响应,请求技术协助

问题排查与修复方案

我帮你找到了几个导致搜索无响应的关键问题,咱们一步步来解决:

1. UISearchBarDelegate方法签名错误(核心问题)

在Swift 3及之后的版本中,搜索栏的代理方法需要遵循新的签名规范(第一个参数前要加下划线),你的代码用的是旧版本签名,导致系统根本不会调用这些代理方法,这就是输入搜索文本后毫无反应的核心原因。

错误签名示例:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

正确签名示例:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

所有搜索栏代理方法都需要修正这个问题,包括:

  • searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
  • searchBarTextDidEndEditing(_ searchBar: UISearchBar)
  • searchBarCancelButtonClicked(_ searchBar: UISearchBar)
  • searchBarSearchButtonClicked(_ searchBar: UISearchBar)

2. 空搜索文本判断逻辑漏洞

你当前只判断了搜索文本等于单个空格的情况,但实际用户可能输入空字符串或多个空格,应该改成判断文本是否为空白字符:

if searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
    searchActive = false
} else {
    // 过滤逻辑
}

3. HTML转义字符语法错误

代码里的->是HTML转义字符,需要替换成Swift原生的箭头符号->,否则会导致编译问题。


修正后的完整代码

import UIKit
import SDWebImage

class ArticleListViewController: UITableViewController, UISearchBarDelegate{
    @IBOutlet weak var searchBar: UISearchBar!
    var searchActive : Bool = false
    var filtered = [Article]()
    var articles = [Article](){
        didSet{
            DispatchQueue.main.async{
                self.tableView.reloadData()
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        searchBar.delegate = self
        downLoadLatestArticles()
    }
    
    func downLoadLatestArticles(){
        Article.downLoadItem { (articles, error) in
            if let error = error {
                print("fail \(error)")
                return
            }
            if let articles = articles {
                self.articles = articles
            }
        }
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchActive = true;
    }
    
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchActive = false;
    }
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false;
        searchBar.text = ""
        tableView.reloadData()
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false;
        searchBar.resignFirstResponder()
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        let trimmedText = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
        if trimmedText.isEmpty {
            searchActive = false
        } else {
            filtered = articles.filter { article in
                let articleName = article.name
                return articleName.range(of: trimmedText, options: .caseInsensitive) != nil
            }
            searchActive = !filtered.isEmpty
        }
        self.tableView.reloadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchActive ? filtered.count : articles.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableCell", for: indexPath) as! ListTableCell
        let article = searchActive ? filtered[indexPath.row] : articles[indexPath.row]
        
        cell.nameLabel?.text = article.name
        cell.locationLabel?.text = article.location
        cell.photoView?.sd_setImage(with: article.image_URL)
        return cell
    }
}

另外我还优化了几个细节:

  • 取消搜索时自动清空搜索栏文本,提升用户体验
  • 点击搜索按钮时收起键盘
  • 用三元运算符简化了表格数据源方法的逻辑

内容的提问来源于stack exchange,提问作者Teddy Chen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:40:28