- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
但它并没有详细说明,无论如何,正如我所希望的那样。我有一个“探索”页面屏幕之类的东西。它的顶部有一个搜索栏,我想在其中间部分显示内容/帖子。但是当用户单击搜索栏时,我想转到 tableview Controller ,替换屏幕上的内容,以显示搜索结果。
我在 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 。我该如何实现这一目标?
最佳答案
您应该设置 UISearchController 的 searchResultsController 和 searchResultsUpdater 属性类
关于 UISearchBar 对象,您必须使用 UISearchController 附带的对象,并以编程方式将其添加到 View 中。
view.addSubview(searchController.searchBar)
关于ios - 单击 UISearchBar 时如何用表格 View 替换屏幕上的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59839647/
这个问题本质上是,UISearchBar在激活时与状态栏重叠。我知道已经有很多类似的问题发布了,我已经尝试了所有方法,但仍然无法解决。 当屏幕初始化时,搜索栏似乎位于正确的位置。当我向下滚动屏幕时,您
我尝试在我的应用程序中实现 iOS 11 中的新搜索栏,但出于某种原因,我无法让搜索栏颜色随导航栏一起更改。在切换到新样式之前,我在 AppDelagate 中使用 UISearchBar.appea
我正在努力做到这一点: Consider a table view with Search Bar on its header.Table view gets refreshed whenever s
documentation表示 UISearchBar我们可以在 tvOS 中使用它(Apple 当然在他们的搜索 View 中使用它),但我在对象库中找不到它。 其他人有同样的问题吗?我没有看到一个
当我在 UISearchBar 上搜索时,我的普通 tableView 已设置为 - (UITableViewCell *)tableView:(UITableView *)tableView cel
这里是我在view controller.h中的代码 #import "AppDelegate.h" @interface SearchRsultsFanSideViewController : UI
我在一个应用程序工作,我今天在 iOS 7.1 上进行了测试。到目前为止,我的搜索栏是正常的: 但现在我有这个问题: 出现一个灰色 View ,我不知道如何删除它,因为我的代码中没有这个 View 。
我正在更新适用于 iOS 7 的应用程序,而我的一项功能(允许在搜索栏中没有文本的情况下激活搜索栏搜索按钮)停止工作。我使用了以下代码。关于如何让它再次工作有什么建议吗?提前致谢。 UITextFie
我有一个带有 UISearchBar(和 SearchDisplayController)的 UIViewController 以及一个 UITableView。当导航到这个 View Control
我有一个我似乎无法克服的小情况。 我有一个 UITableView,它的顶部嵌入了一个 UISearchBar。 TableView 由 NSFetchedResultsController 和 Co
我在 TableView 顶部使用带有 UISearchBar 的 UITableView,对我来说,一切都工作正常,只有一个问题,比如如果我搜索一个字母,uitableview 中的搜索结果被过滤,
表格标题中有一个搜索栏。当用户在 iOS 7 上快速点击它两次时,它就会消失。有人对我们做错了什么有什么建议吗? 最佳答案 经过大量的试验和错误,我发现当searchDisplayController
我想更改 UISearchBar。文本字段的高度和宽度。我的问题是如何更改 iphone 中 UISearchBar 中的 UiSearchbar 高度、宽度、颜色 和 Uitextfield 高度?
这两张图应该可以说明了吧。第一个正常显示我的 UITableView。第二个显示当我触摸 UISearchBar 并且它获得焦点时。为什么单元格标题和搜索栏之间有间隙?有人知道如何解决这个问题吗? i
我有一个 UISearchBar添加到 UITableView 的顶部. // init search bar self.resultSearchController = ({
在 iOS 5 上使用带有 showCancelButton=YES 的 UISearchBar。希望在键盘下拉时取消按钮保持启用状态。使用以下代码似乎不起作用: for (id subView in
我有一个 UITableView,它的数据源和委托(delegate)不是由放置它的 View Controller 处理的,而是由另一个名为 AbstractInviteFriendsDataSou
我研究过这个问题。看起来这是一个常见问题,但我尝试过的任何方法都没有奏效。我对 iPhone 应用程序开发和 objective-c 非常陌生。此时我想做的就是在搜索栏字段中输入一个字符串,并在点击键
我有一个使用 UISearchBar 的搜索功能,该功能是即时发生的,所以我认为用“完成”替换键盘上的“搜索”按钮会更明显。 有办法做到这一点吗? 谢谢 最佳答案 您可以更改 UISearchBar
有没有人设法为 UISearchBar 的范围栏部分着色,它有一个 tintColor 属性,但设置它不会影响附加的范围栏 UISegmentedControl。我在酒吧上有一个 Handlebars
我是一名优秀的程序员,十分优秀!