ios - tableView.hidden 破坏其他元素

标签 ios swift uitableview mkmapview uisearchbar

我在我的 iOS 应用程序中有一个 mapView 和一个 UISearchBar 来搜索位置并将用户置于其中心。我的搜索结果由自动完成生成并显示在 tableView 中。

我遇到了麻烦,当我点击取消时,它在 tableView 中显示了我的所有数据。

所以我实现了这个功能:

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    searchTableView.hidden = true
}

但是那个函数的渲染使得这个:

enter image description here

我不知道(也不知道如何查看)我的 mapView 是否仍在黑屏下。

顺便说一句,如果我删除我的取消按钮功能,然后搜索一些东西,如果我点击取消,它会显示如下内容:

enter image description here

要再次隐藏我的 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)
        }
    }
}

这是来 self 的 ViewController.swift 文件。 我的 searchBar 变量是一个自定义类,它包含一个 NSArray 数据和另一个 fileterDatas

@IBOutlet weak var mapView: UGOMapController!
@IBOutlet var searchTableView: UITableView!
@IBOutlet weak var searchBar: UGOSearchBar!

感谢您的帮助。 如果您需要我的更多代码,我可以编辑这篇文章。

编辑:

这是我的 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
}

还有我的层次结构:

enter image description here

还有我的 viewController 检查器:

enter image description here enter image description here

最佳答案

您有 3 个不同的 socket 连接到表格 View 。

这意味着当你隐藏 tableView 时,你也隐藏了 View ,因为它连接到同一个 Search Table View

你必须确保你的表格 View 只连接到一个 socket 。

编辑

更准确地说:

  1. 您必须将 View Controller 从 UITableViewController 更改为 UIViewController。

  2. view socket 将连接到 viewController 的 view 属性。

  3. tableView outlet 将被删除,因为在普通的 UIViewController 中不存在此属性。 (在 UITableViewController 中定义)

  4. SearchTableView 应该连接到您在 View Controller 中定义的 searchTableView 属性。

关于ios - tableView.hidden 破坏其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38655426/

相关文章:

iphone - 自动调整 UITableViewCell 内容宽度

ios - 如何将ChildViewController添加到UITableViewController?

ios - 如何获取正在点击的单元格的索引路径?

ios - 如何在WKWebview上添加UIButton?

ios - 移动 View 时安全区域插入消失

ios - swift 3 : How to center horizontally the last row of cells in a UICollectionView with only one section?

ios - ?尝试将数据源加载到 UIPickerView 时返回

ios - 添加准备功能后导航栏消失

ios - 为特定 View Controller ios6 设置 UIBarButtonItem 的色调颜色

uitableview - 向 SKScene 添加一个小的 UIView