【发布时间】: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。