【问题标题】:swift link how all searchbar results to same viewcontroller快速将所有搜索栏结果如何链接到同一个视图控制器
【发布时间】:2019-11-18 10:14:42
【问题描述】:

我有一个 SearchViewController。 SearchViewController 的图像是:-

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”时,它工作正常,如下图所示:-

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

【问题讨论】:

    标签: ios swift segue searchbar


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

    【讨论】:

    • 在哪里使用它?我不想使用导航控制器
    • 1.您答案的第一部分是错误的,可能是因为您错过了一点。我需要向右滑动。 2.您的答案的第二部分对我有所帮助,尽管您应该已经指定,即最快的方法是-”单击“Tablecell”,转到其属性检查器并将其附件设置为“披露指示器”。它将右箭头图像添加到 TableCell。"
    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2020-10-08
    • 2020-10-26
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    相关资源
    最近更新 更多