gpt4 book ai didi

ios - 快速链接所有搜索栏结果到同一个 View Controller

转载 作者:行者123 更新时间:2023-11-28 23:25:12 24 4
gpt4 key购买 nike

我有一个 SearchViewController。 SearchViewController 的图像是:- enter image description here

SearchViewController 的代码是:-

import UIKit  
class SearchViewController: UIViewController, UITableViewDataSource, UISearchBarDelegate {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
let data = ["New York, NY", "Los Angeles, CA", "Chicago, IL", "Houston, TX",
"Philadelphia, PA", "Phoenix, AZ", "San Diego, CA", "San Antonio, TX",
"Dallas, TX", "Detroit, MI", "San Jose, CA", "Indianapolis, IN",
"Jacksonville, FL", "San Francisco, CA", "Columbus, OH", "Austin, TX",
"Memphis, TN", "Baltimore, MD", "Charlotte, ND", "Fort Worth, TX"]


var filteredData: [String]!

override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
searchBar.delegate = self
filteredData = data
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = filteredData[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
// This method updates filteredData based on the text in the Search Box
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let searchT = searchText.count < 2 // will give sulggestion when at least 2 chrs in box
filteredData = searchT ? data : data.filter { (item: String) -> Bool in
// If dataItem matches the searchText, return true to include it
return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
}
func updateSearchResults(for searchBar: UISearchBar) {
view.isHidden = true
}
tableView.reloadData()
}
}

“SearchViewController”工作正常,没问题。现在,我试图通过 segue 将“SearchViewController”链接到另一个 ViewController,比如“AllSegueViewController”,它位于“SearchViewController”的右侧。 Segue 的名称是“PickUpSegueFromRight”(参见上面的第一张图片和下面的代码)。 segue 通常工作正常,即向右滑动,但在这种情况下不是。

import UIKit

class PickUpSegueFromRight: UIStoryboardSegue {
override func perform() {
let src = self.source
let dst = self.destination

src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransform(translationX: +src.view.frame.size.width, y: 0)

UIView.animate(withDuration: 0.25,
delay: 0.0,
options: [.curveEaseIn, .curveEaseOut],
animations: {
dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
}, completion: { finished in
src.present(dst, animated: false, completion: nil)
})
}
}

当我在模拟器上运行“SearchViewController”时,它工作正常,如下图所示:- enter image description here

我的问题:- 我的第一个也是主要问题是我应该在“SearchViewController”和/或“PickUpSegueFromRight”和/或“AllSegueViewController”中添加什么,以便当我单击任何搜索结果时,例如。 “Chicago, IL”(见上面的模拟器图片),它应该转到“AllSegueViewController”?其次,我想在“AllSegueViewController”中添加/附加搜索结果。我的第二个小问题是我还想向所有搜索结果行添加箭头图像,例如。 “芝加哥,伊利诺伊州”等,因此它应该建议用户点击它应该向右滑动到“AllSegueViewController”。

最佳答案

  1. 无需使用 PickUpSegueFromRight 类,更简单的方法是单击转场并在属性检查器中设置转场类型以呈现或推送
  2. 您可以自定义 UITableViewCell 或默认在 UITableviewcell 中使用“披露指示器”

关于ios - 快速链接所有搜索栏结果到同一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58912223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com