【问题标题】:Search Functionality Implemented in a transparent UITableView but it is not working. Any help is appreciated搜索功能在透明的 UITableView 中实现,但它不起作用。任何帮助表示赞赏
【发布时间】:2020-10-12 04:15:13
【问题描述】:

` 这是我用代码实现搜索功能的代码。没有使用故事板。 我运行程序并看到搜索栏。 我尝试搜索元素,但没有出现。 我将代码调试为“PO 搜索”-> 我得到的响应为“False” 所以我理解搜索功能代码没有在“索引路径行的单元格”中执行 我尽力调试代码。 我无法在代码中找到错误。 我怎么解决这个问题?我在哪里失踪。任何帮助表示赞赏..

这是我用代码实现搜索功能的代码。没有使用故事板。 我运行程序并看到搜索栏。 我尝试搜索元素,但没有出现。 我将代码调试为“PO 搜索”-> 我得到的响应为“False” 所以我理解搜索功能代码没有在“索引路径行的单元格”中执行 我尽力调试代码。 我无法在代码中找到错误。 我怎么解决这个问题?我在哪里失踪。任何帮助表示赞赏..`

import UIKit

class Cell : UITableViewCell {
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{
  
    @IBOutlet weak var selectFruit: UIButton! 
    
    let transparentView = UIView()
    let tableView = UITableView()
    var button = UIButton()
    
    var data = [String]()
    
    var searchFruit = [String]()
    var searching : Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self

// Register the Cell

        tableView.register(Cell.self, forCellReuseIdentifier: "Cell")
    }
      
// Adding the transparent View to show array elements
  
    func addTransparentView(frame : CGRect) {
        transparentView.frame = self.view.frame
        self.view.addSubview(transparentView)
        tableView.frame = CGRect(x: frame.origin.x, y: frame.origin.y + frame.height, width: frame.width, height: 0)
        
// Implementing Search bar functionality 

        var searchBar : UISearchBar = UISearchBar()
        searchBar.sizeToFit()
        searchBar = UISearchBar(frame: CGRect(x: 10, y: 20, width: tableView.frame.width-20, height: tableView.frame.height-20))
        searchBar.searchBarStyle = UISearchBar.Style.prominent
        searchBar.placeholder = " Search..."
        searchBar.sizeToFit()
        searchBar.isTranslucent = false
        searchBar.backgroundImage = UIImage()
        searchBar.delegate = self
               
        self.view.addSubview(tableView)
        tableView.addSubview(searchBar)
        tableView.layer.cornerRadius = 5
        transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.9)
        tableView.reloadData()
        
        let tapgesture = UITapGestureRecognizer(target: self, action: #selector(deleteTransparentView))
        transparentView.addGestureRecognizer(tapgesture)
        transparentView.alpha = 0
        
// Animating the transparent view 

        UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            self.transparentView.alpha = 0.5
            self.tableView.frame = CGRect(x: frame.origin.x, y: frame.origin.y + frame.height + 5, width: frame.width, height: CGFloat(self.data.count * 50))
        }, completion: nil)
    }
    
    @objc func deleteTransparentView() {
        let frame = button.frame
        UIView.animate(withDuration: 0.4, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            self.transparentView.alpha = 0
            self.tableView.frame = CGRect(x: frame.origin.x, y: frame.origin.y + frame.height, width: frame.width, height: 0)
        }, completion: nil)
    }
   
// Action methods along with array elements 
    @IBAction func selectFruit(_ sender: Any) {
        data = ["Apple","Apple1","Apple2","Apple3","Apple4","Oranges","Ape","Banana","Grapes","Kiwi","Kite","JackFruit"]
        button = selectFruit
        addTransparentView(frame: selectFruit.frame)
    }
    
    // MARK:- TableView Methods
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
          return searchFruit.count
        } else {
            return data.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        if searching {
            cell.textLabel?.text = searchFruit[indexPath.row]
        } else {
            cell.textLabel?.text = data[indexPath.row]
        }
        return cell
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange textSearched: String) {
        searchFruit = data.filter({$0.prefix(textSearched.count) == textSearched})
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        button.setTitle(data[indexPath.row], for: .normal)
        deleteTransparentView()
    }
}

【问题讨论】:

  • 搜索后什么时候重新加载table
  • 你在哪里设置 search = true?
  • @Rob。您能否让我确切地知道我必须在哪里重新加载表格以及我必须在哪里将值设置为 true。

标签: swift swift4 swift5


【解决方案1】:

您必须设置searching true 并重新加载表格。

func searchBar(_ searchBar: UISearchBar, textDidChange textSearched: String) {
    searchFruit = data.filter({$0.prefix(textSearched.count) == textSearched})
    
        searching = !searchFruit.isEmpty
        tableView.reloadData()

}

【讨论】:

  • 感谢 Bob 和 Vadian。我将建议的更改添加到我的代码中。过滤元件工作得很好。但它只过滤第一个字符。它没有使用我输入的所有字符进行过滤。另一个问题是空的 tableviewcell 出现在两者之间。为此,我使用了tableView.tableFooterView = UIView() tableView.tableHeaderView = UIView() 但它并没有删除中间的空单元格。如何解决这些问题请提供任何帮助
  • 这是您必须提出的另一个问题,如果这些答案中的任何一个帮助您将它们标记为正确并给予支持。
【解决方案2】:

您必须在textDidChange 中相应地设置searching,并且有一种更有效的方法来过滤数据

func searchBar(_ searchBar: UISearchBar, textDidChange textSearched: String) {
    if textSearched.isEmpty {
        searching = false
        searchFruit.removeAll()
    } else {
        searching = true
        searchFruit = data.filter{$0.range(of: textSearched, options: [.caseInsenitive, .anchored]) != nil}
    }
    tableView.reloadData()
}

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多