gpt4 book ai didi

ios - 如何防止搜索栏在滚动时消失? iOS swift

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:10 25 4
gpt4 key购买 nike

我正在创建一个联系人应用程序。我的 TableView 顶部有一个滚动条。

You see a search bar

当我向下滚动时,搜索栏消失了。

Now you dont

如何防止搜索栏在滚动时消失?我希望它始终保持在页面顶部,就像第一张图片一样。

这是我的 Storyboard的图片:

enter image description here

如果解决方案不在 Storyboard 中,这是我的 View Controller 代码:

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {

//manages search bar
var searchController:UISearchController!

var contacts = [Contact]()

//array to hold contacts that match the search results
var filteredContacts = [Contact]()

override func viewDidLoad() {

super.viewDidLoad()

//initialize the defaults manager class
NSUserDefaultsManager.initializeDefaults()

//search controller
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false

//load the contacts
title = "Contacts"

tableView.backgroundView = UIImageView(image: UIImage(named: "valblur15"))

contacts = [Contact]()
let api = ContactAPI()
api.loadContacts(didLoadContacts)

}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

//reload the table view to check for new contacts
tableView.reloadData()

//set the color of the nav bar to valbruna yellow
navigationController?.navigationBar.backgroundColor = UIColor(red: 254.0/255.0, green: 196.0/255.0, blue: 37.0/255.0, alpha: 0.95)

}

//MARK: -Helper Methods

// Uupdate searching results
func updateSearchResultsForSearchController(searchController: UISearchController) {

let searchText = searchController.searchBar.text
filterContentForSearchText(searchText)
tableView.reloadData()

}

func filterList() { // should probably be called sort and not filter

//sort contacts from a-z
contacts.sort() { $0.name < $1.name }

//remove contacts whose locations are nil
contacts = contacts.filter() { $0.location != "nil"}

//remove contacts whose titles phone email and department are all nil
contacts = contacts.filter() {
if($0.title != "" || $0.phone != "" || $0.email != "" || $0.department != ""){
return true
}
return false
}


tableView.reloadData()
}

func didLoadContacts(contacts: [Contact]){
self.contacts = contacts
filterList()
tableView.reloadData()
}

//MARK: -Table View

//set number opf sections in table view
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

//delegeate that tells tabel view how many cells to have
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return size of array

//if searching show count of filtered contacts
if (searchController.active){

return self.filteredContacts.count

}else{

return self.contacts.count

}

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = self.tableView.dequeueReusableCellWithIdentifier("customcell") as! CustomCell //from the customcell class

//change color of cell text label
cell.textLabel?.textColor = UIColor.blackColor()
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.8)

let contact : Contact

//if users is searching then return filtered contacts
if (searchController.active){

contact = self.filteredContacts[indexPath.row]

}else{

contact = self.contacts[indexPath.row]

}


//handel assignment of text
cell.textLabel?.text = contact.name

//retrieve from customcell class
cell.contact = contact

return cell
}


//MARK: -Search

func filterContentForSearchText(searchText: String, scope: String = "Title")
{
self.filteredContacts = self.contacts.filter({( contact: Contact) -> Bool in

//filters contacts array

var categoryMatch = (scope == "Title")
var stringMatch = contact.name?.rangeOfString(searchText)

return categoryMatch && (stringMatch != nil)

})
}

func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchString searchString: String!) -> Bool {

self.filterContentForSearchText(searchString, scope: "Title")

return true

}


func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {

self.filterContentForSearchText(self.searchController!.searchBar.text, scope: "Title")

return true

}

//MARK: -Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if(segue.identifier == "detailview"){
let cell = sender as! CustomCell
let detailView = segue.destinationViewController as! DetailViewController
detailView.preContact = cell.contact

}
}

}

编辑 1:

以下是我根据 Ryosuke Hiramatsu 的解决方案采取的步骤:

  1. 命令 X TableView 。
  2. 添加 View 。
  3. 命令 V TableView 进入新 View 。
  4. 在 View Controller 中,将 UITableViewController 更改为 UITableView
  5. 向名为 TableView 的 View Controller 添加一个导出。
  6. 删除 TableView 函数中的覆盖。
  7. 删除这行代码:tableView.tableHeaderView = searchController.searchBar
  8. 返回 Storyboard并将搜索栏移出表格 View 。它会出现在表格 View 的后面。
  9. 向下移动表格 View 并向上移动搜索栏。
  10. 添加必要的约束条件。

我的 View 现在看起来像这样:

2 Search Bars

当我向下滚动时:

1 Search Bar

我的 Storyboard:

Storyboard edit 1

最后是我更新的 View Controller 代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {


@IBOutlet var tableView: UITableView!


//manages search bar
var searchController:UISearchController!

var contacts = [Contact]()

//array to hold contacts that match the search results
var filteredContacts = [Contact]()

override func viewDidLoad() {

super.viewDidLoad()

//initialize the defaults manager class
NSUserDefaultsManager.initializeDefaults()

//search controller
searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.sizeToFit()
definesPresentationContext = true

tableView.tableHeaderView = searchController.searchBar

searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false

//load the contacts
title = "Valbruna Contacts"

tableView.backgroundView = UIImageView(image: UIImage(named: "valblur15"))

contacts = [Contact]()
let api = ContactAPI()
api.loadContacts(didLoadContacts)

}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

//reload the table view to check for new contacts
tableView.reloadData()

//set the color of the nav bar to valbruna yellow
navigationController?.navigationBar.backgroundColor = UIColor(red: 254.0/255.0, green: 196.0/255.0, blue: 37.0/255.0, alpha: 0.95)

}

//MARK: -Helper Methods

// Uupdate searching results
func updateSearchResultsForSearchController(searchController: UISearchController) {

let searchText = searchController.searchBar.text
filterContentForSearchText(searchText)
tableView.reloadData()

}

func filterList() { // should probably be called sort and not filter

//sort contacts from a-z
contacts.sort() { $0.name < $1.name }

//remove contacts whose locations are nil
contacts = contacts.filter() { $0.location != "nil"}

//remove contacts whose titles phone email and department are all nil
contacts = contacts.filter() {
if($0.title != "" || $0.phone != "" || $0.email != "" || $0.department != ""){
return true
}
return false
}


tableView.reloadData()
}

func didLoadContacts(contacts: [Contact]){
self.contacts = contacts
filterList()
tableView.reloadData()
}

//MARK: -Table View

//set number opf sections in table view
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

//delegeate that tells tabel view how many cells to have
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return size of array

//if searching show count of filtered contacts
if (searchController.active){

return self.filteredContacts.count

}else{

return self.contacts.count

}

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = self.tableView.dequeueReusableCellWithIdentifier("customcell") as! CustomCell //from the customcell class

//change color of cell text label
cell.textLabel?.textColor = UIColor.blackColor()
cell.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.8)

let contact : Contact

//if users is searching then return filtered contacts
if (searchController.active){

contact = self.filteredContacts[indexPath.row]

}else{

contact = self.contacts[indexPath.row]

}


//handel assignment of text
cell.textLabel?.text = contact.name

//retrieve from customcell class
cell.contact = contact

return cell
}


//MARK: -Search

func filterContentForSearchText(searchText: String, scope: String = "Title")
{
self.filteredContacts = self.contacts.filter({( contact: Contact) -> Bool in

//filters contacts array

var categoryMatch = (scope == "Title")
var stringMatch = contact.name?.rangeOfString(searchText)

return categoryMatch && (stringMatch != nil)

})
}

func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchString searchString: String!) -> Bool {

self.filterContentForSearchText(searchString, scope: "Title")

return true

}


func searchDisplayController(controller: UISearchController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {

self.filterContentForSearchText(self.searchController!.searchBar.text, scope: "Title")

return true

}

//MARK: -Segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if(segue.identifier == "detailview"){
let cell = sender as! CustomCell
let detailView = segue.destinationViewController as! DetailViewController
detailView.preContact = cell.contact

}
}

我现在的问题是只有第一个搜索栏是静止的,当我输入时它不搜索。第二个搜索栏在滚动时消失。此外,当我单击一个名称时,它不再转到下一个 View 。

最佳答案

问题是 View 层次结构。

你的 Storyboard是这样的:

enter image description here

在 TableView 上添加了搜索栏。

正确的层次结构是这样的:

enter image description here

您将 UITableViewController 更改为 UIViewController,并附加 SearchDisplayController,同时注意 View 层次结构。

它会起作用:)

关于ios - 如何防止搜索栏在滚动时消失? iOS swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31702413/

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