【问题标题】:tableView.hidden breaks other elementstableView.hidden 打破了其他元素
【发布时间】:2016-07-29 09:36:54
【问题描述】:

我在我的 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 变量是一个自定义类,其中包含一个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
}

还有我的层次结构:

还有我的 viewController 检查器:

【问题讨论】:

  • 您可以在 Xcode 中检查视图层次结构:developer.apple.com/library/ios/documentation/ToolsLanguages/… 我还建议检查 searchTableView 是否设置正确。
  • 我刚刚检查了视图层次结构(感谢提示^^),但不,在那一点上一切都是正确的......另一方面,我有我的 numberOfRowInSection / cellForRowAtIndex ... 功能。我对它们进行了自定义,所以这是可能的,但我对 swift 完全陌生,无法真正看到其中有什么问题。我应该把它放在帖子里吗?
  • 是的,请。了解视图层次结构以及如何设置出口可能会有所帮助
  • 在这里。谢谢:)
  • 能否请您也向您的ViewController 显示连接检查器?我想知道您是否不小心将 tableview 和 mapView 连接到同一个插座。

标签: ios swift uitableview mkmapview uisearchbar


【解决方案1】:

您有 3 个不同的插座连接到表视图。

意思是当你隐藏tableView时,你也隐藏了view,因为它连接到同一个Search Table View

您必须确保将表格视图仅连接到一个插座。

编辑

更准确地说:

  1. 您必须将视图控制器从 UITableViewController 更改为 UIViewController。

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

  3. tableView 插座将被移除,因为在普通 UIViewController 中该属性不存在。 (在 UITableViewController 中定义)

  4. SearchTableView 应该连接到您在视图控制器中定义的 searchTableView 属性。

【讨论】:

  • 谢谢。但是我如何将它们链接到其他视图。或者更像,我将它们链接到什么?我的 tableView 和我的 searchTableView 是两个不同的东西吗?我只是添加了我可以制作的每个链接的图片^^
  • 更新了我的答案 :) 希望对您有所帮助。这篇文章也可能对如何在 xib 中的普通 UIViewController 中设置表格视图有所帮助:weheartswift.com/…
  • 但是如果我将控制器更改为 UIView,我将无法覆盖 ly tableView 函数。我应该在旁边创建一个 UITableViewController 文件吗?
  • 您不必覆盖它们。由于您将视图控制器设置为表视图的委托和数据源,因此您只需在视图控制器中实现这些方法。在您的情况下,您只需要删除 override 关键字:)不要改成UIView,改成UIViewController: UITableViewDelegate, UITableViewDataSource
  • 您还必须在 xib 中重新创建层次结构。您需要将表视图作为子视图添加到 viewController 的视图中。搜索栏也是如此。请点击我在上面的 cmets 中发布的链接。
猜你喜欢
  • 2013-07-16
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 1970-01-01
相关资源
最近更新 更多