gpt4 book ai didi

ios - 单击 UISearchBar 时如何用表格 View 替换屏幕上的内容

转载 作者:行者123 更新时间:2023-11-29 05:09:15 24 4
gpt4 key购买 nike

我知道以前有过这样的问题:herehere

但它并没有详细说明,无论如何,正如我所希望的那样。我有一个“探索”页面屏幕之类的东西。它的顶部有一个搜索栏,我想在其中间部分显示内容/帖子。但是当用户单击搜索栏时,我想转到 tableview Controller ,替换屏幕上的内容,以显示搜索结果。

enter image description here

我在 UIView 容器中嵌入了一个 tableviewcontroller:

import UIKit
import Firebase

class SearchTableViewController: UITableViewController, UISearchResultsUpdating {


let searchController = UISearchController(searchResultsController: nil)
@IBOutlet var searchResultsTableView: UITableView!

var usersArray = [NSDictionary?]()
var filteredIsers = [NSDictionary?]()
var ref = Database.database().reference()

override func viewDidLoad() {
super.viewDidLoad()

searchController.searchResultsUpdater = self
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar

ref.child("users").child("public").queryOrdered(byChild: "username").observe(.childAdded, with: { (snapshot) in

if let user = snapshot.value as? [String:Any]{
let username = user["username"] as? String ?? ""
if(username == SpalshScreenViewController.UserData.username){
return
}else{
self.usersArray.append(snapshot.value as? NSDictionary)
}
}else{
return
}


print(self.usersArray)

// insert rows

self.searchResultsTableView.insertRows(at: [IndexPath(row: self.usersArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic )
})

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if searchController.isActive && searchController.searchBar.text != "" {
return filteredIsers.count
}else{
return self.usersArray.count
}
}

func updateSearchResults(for searchController: UISearchController) {
filterContent(searchText: self.searchController.searchBar.text!)
}

func filterContent(searchText: String){
self.filteredIsers = self.usersArray.filter{ user in
let username = user!["username"] as? String

return(username?.lowercased().contains(searchText.lowercased()))!
}
tableView.reloadData()
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

let user : NSDictionary?

if searchController.isActive && searchController.searchBar.text != "" {
user = filteredIsers[indexPath.row]
}else{
user = self.usersArray[indexPath.row]
}

cell.textLabel?.text = user?["firstName"] as? String
cell.detailTextLabel?.text = user?["username"] as? String
let profilePicUrl = user?["profilePicURL"] as? String
let encodedurl = URL(string: profilePicUrl!)
print(profilePicUrl)
cell.imageView!.kf.setImage(with: encodedurl, placeholder: UIImage(named: "profile_pic"))

return cell
}
}

我是否可以将该代码放在单独的 UITableViewController 中,然后在单击搜索栏时显示该 View ?

我知道您会将 let searchController = UISearchController(searchResultsController: nil) 更改为 let searchController = UISearchController(searchResultsController: tableViewController)

但我不知道如何将所有内容绑定(bind)在一起。在我的 searchViewController 中,我引用了 UISearchBar,我想在它上面添加一个点击手势并 present 单击时的 tablecontrollerview 但这意味着动画和搜索栏样式会不一致?

编辑:

所以基本上,我有一个 View Controller ,在该 View Controller 中,我在顶部有一个搜索栏(目前什么也不做),在该搜索栏下方将是一个充满不同帖子的 Collection View 。当用户单击搜索栏时,我想用包含搜索结果的 TableView 替换该 Collection View 。我该如何实现这一目标?

最佳答案

您应该设置 UISearchControllersearchResultsControllersearchResultsUpdater 属性类

关于 UISearchBar 对象,您必须使用 UISearchController 附带的对象,并以编程方式将其添加到 View 中。

view.addSubview(searchController.searchBar)

关于ios - 单击 UISearchBar 时如何用表格 View 替换屏幕上的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59839647/

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