Ios 如何以编程方式;轻敲;当Swift中仅显示一个结果时,UITableview单元格
我有一个iOS应用程序,可以在UItableview中显示搜索栏中的搜索结果。目前,用户必须点击其中一个搜索结果才能进行搜索。我想做的是,如果搜索结果只显示一个结果,那么segue会自动发生。下面是我当前的代码Ios 如何以编程方式;轻敲;当Swift中仅显示一个结果时,UITableview单元格,ios,swift,Ios,Swift,我有一个iOS应用程序,可以在UItableview中显示搜索栏中的搜索结果。目前,用户必须点击其中一个搜索结果才能进行搜索。我想做的是,如果搜索结果只显示一个结果,那么segue会自动发生。下面是我当前的代码 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let vc = segue.destination as! ProductViewController let pro
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,您可以将检查放在委托方法中;当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
}
我试着使用你建议的代码,但是segue会继续自动重复它自己,而不仅仅是执行segue我已经更新了section方法中的行数,在视图控制器中定义变量var isRedirected:Bool=false,并在每次重新加载表视图时将其设置为false。使用建议的代码时,我会出现以下错误:线程1:EXC\U BAD\U指令(代码=EXC\U I386\U INVOP,子代码=0x0)
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)
}
}