tableView.hidden 打破其他元素
tableView.hidden breaks other elements
我的 iOS 应用程序中有一个 mapView
和一个 UISearchBar
用于搜索位置并将用户置于其中心。我的搜索结果由自动完成生成并显示在 tableView
.
中
我遇到了麻烦,当我点击取消时,它显示了我在 tableView
中的所有数据。
所以我实现了这个功能:
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchTableView.hidden = true
}
但是那个函数的渲染使得这个:
我不知道(也不知道怎么看)我的mapView
是否还在黑屏
顺便说一句,如果我删除我的取消按钮功能,然后搜索一些东西,如果我点击 Cancel
,它会显示如下内容:
要再次隐藏我的 tableView
,我必须进行另一次搜索,而不是使用小十字按钮将其删除,然后取消。
我的自动完成搜索代码是这样的:
func initTableView() {
searchTableView.delegate = self
searchTableView.dataSource = self
searchTableView.scrollEnabled = true
searchTableView.hidden = true
}
func searchBarSearchButtonClicked() {
searchTableView.hidden = false
searchBar.resignFirstResponder()
dismissViewControllerAnimated(true, completion: nil)
searchBar(searchBar, textDidChange: searchBar.text!)
}
func searchBar(_searchBar: UISearchBar, textDidChange searchText: String) {
searchTableView.reloadData()
searchAutocompleteEntriesWithSubstring(searchText)
}
func searchAutocompleteEntriesWithSubstring(substring: String) {
searchBar.filtredDatas.removeAll(keepCapacity: false)
for curString in searchBar.datas {
let myString:NSString! = curString.title! as NSString
let substringRange :NSRange! = myString.rangeOfString(substring)
if (substringRange.location == 0) {
searchBar.filtredDatas.append(curString)
}
}
}
这是我的 ViewController.swift
文件。
我的 searchBar
变量是一个自定义 class,其中包含一个 NSArray
数据和另一个 fileterDatas
数据。
@IBOutlet weak var mapView: UGOMapController!
@IBOutlet var searchTableView: UITableView!
@IBOutlet weak var searchBar: UGOSearchBar!
感谢您的帮助。
如果您需要我的更多代码,我可以编辑此 post.
编辑:
这是我的 UITableView 函数,用于 searchTableView
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchBar.filtredDatas.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let autoCompleteRowIdentifier = "AutoComplete"
var cell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier) as UITableViewCell!
if cell != nil {
let index = indexPath.row as Int
if (searchBar.filtredDatas.count > 0) {
cell!.textLabel!.text = searchBar.filtredDatas[index].title
}
}
else {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: autoCompleteRowIdentifier)
}
return cell!
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
searchBar.text = selectedCell.textLabel!.text
}
我的层次结构:
还有我的 viewController 检查员:
您有 3 个不同的插座连接到 table 视图。
这意味着当你隐藏table视图时,你也隐藏了视图,因为它连接到同一个Search Table View
您必须确保只将 table 视图连接到一个插座。
编辑
更准确地说:
您必须将视图控制器从 UITableViewController 更改为 UIViewController。
view
插座将连接到 viewController 的 view
属性。
tableView
出口将被删除,因为在普通的 UIViewController 中这个 属性 不存在。 (它在 UITableViewController 中定义)
SearchTableView
应该连接到您在视图控制器中定义的 searchTableView
属性。
我的 iOS 应用程序中有一个 mapView
和一个 UISearchBar
用于搜索位置并将用户置于其中心。我的搜索结果由自动完成生成并显示在 tableView
.
我遇到了麻烦,当我点击取消时,它显示了我在 tableView
中的所有数据。
所以我实现了这个功能:
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchTableView.hidden = true
}
但是那个函数的渲染使得这个:
我不知道(也不知道怎么看)我的mapView
是否还在黑屏
顺便说一句,如果我删除我的取消按钮功能,然后搜索一些东西,如果我点击 Cancel
,它会显示如下内容:
要再次隐藏我的 tableView
,我必须进行另一次搜索,而不是使用小十字按钮将其删除,然后取消。
我的自动完成搜索代码是这样的:
func initTableView() {
searchTableView.delegate = self
searchTableView.dataSource = self
searchTableView.scrollEnabled = true
searchTableView.hidden = true
}
func searchBarSearchButtonClicked() {
searchTableView.hidden = false
searchBar.resignFirstResponder()
dismissViewControllerAnimated(true, completion: nil)
searchBar(searchBar, textDidChange: searchBar.text!)
}
func searchBar(_searchBar: UISearchBar, textDidChange searchText: String) {
searchTableView.reloadData()
searchAutocompleteEntriesWithSubstring(searchText)
}
func searchAutocompleteEntriesWithSubstring(substring: String) {
searchBar.filtredDatas.removeAll(keepCapacity: false)
for curString in searchBar.datas {
let myString:NSString! = curString.title! as NSString
let substringRange :NSRange! = myString.rangeOfString(substring)
if (substringRange.location == 0) {
searchBar.filtredDatas.append(curString)
}
}
}
这是我的 ViewController.swift
文件。
我的 searchBar
变量是一个自定义 class,其中包含一个 NSArray
数据和另一个 fileterDatas
数据。
@IBOutlet weak var mapView: UGOMapController!
@IBOutlet var searchTableView: UITableView!
@IBOutlet weak var searchBar: UGOSearchBar!
感谢您的帮助。 如果您需要我的更多代码,我可以编辑此 post.
编辑:
这是我的 UITableView 函数,用于 searchTableView
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchBar.filtredDatas.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let autoCompleteRowIdentifier = "AutoComplete"
var cell = tableView.dequeueReusableCellWithIdentifier(autoCompleteRowIdentifier) as UITableViewCell!
if cell != nil {
let index = indexPath.row as Int
if (searchBar.filtredDatas.count > 0) {
cell!.textLabel!.text = searchBar.filtredDatas[index].title
}
}
else {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: autoCompleteRowIdentifier)
}
return cell!
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedCell : UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
searchBar.text = selectedCell.textLabel!.text
}
我的层次结构:
还有我的 viewController 检查员:
您有 3 个不同的插座连接到 table 视图。
这意味着当你隐藏table视图时,你也隐藏了视图,因为它连接到同一个Search Table View
您必须确保只将 table 视图连接到一个插座。
编辑
更准确地说:
您必须将视图控制器从 UITableViewController 更改为 UIViewController。
view
插座将连接到 viewController 的view
属性。tableView
出口将被删除,因为在普通的 UIViewController 中这个 属性 不存在。 (它在 UITableViewController 中定义)SearchTableView
应该连接到您在视图控制器中定义的searchTableView
属性。