如何在 Swift 中仅显示一个结果时以编程方式"tap" UITableview 单元格



我有一个iOS应用程序,它在UItableview中显示搜索栏中的搜索结果。目前,用户必须点击其中一个搜索结果才能出现segue。我想做的是,如果搜索结果只显示一个结果,则segue会自动发生。下面是我当前的代码。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! ProductViewController
let product:Product!
if(isSearchActive){
product = filterProducts[(tblProducts.indexPathForSelectedRow?.row)!]
} else {
product = products[(tblProducts.indexPathForSelectedRow?.row)!]
}
vc.product = product
}
}
extension SearchViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(isSearchActive){
return filterProducts.count
}
return products.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductTableViewCell") as! ProductTableViewCell
let product:Product!
if(isSearchActive){
product = filterProducts[indexPath.row]
} else {
product = products[indexPath.row]
}
cell.initUI(product)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "gotoProduct", sender: self)
}

假设搜索者正确地重新加载了表视图,则可以将检查放入委托方法中;当tableview检查行数时,如果您正在搜索并且只有一个结果,则执行segue。

extension SearchViewController: UITableViewDelegate, UITableViewDataSource{ 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(isSearchActive){
if filterProducts.count == 1 {
performSegue(withIdentifier: "gotoProduct", sender: self)
}
return filterProducts.count 
} 
return products.count 
}
extension SearchViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(isSearchActive){
if self.isRedirected == false {
self.isRedirected = true
if self.filterProducts.count == 1{
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) { [unowned self] in
self.tblProducts.selectRow(at: IndexPath.init(row: 0, section: 0), animated: true, scrollPosition: .middle)
self.performSegue(withIdentifier: "gotoProduct", sender: self)
}
}
}
return filterProducts.count
}
return products.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductTableViewCell") as! ProductTableViewCell
let product:Product!
if(isSearchActive){
product = filterProducts[indexPath.row]
} else {
product = products[indexPath.row]
}
cell.initUI(product: product)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "gotoProduct", sender: self)
}
}

最新更新