【问题标题】:How to show and hide the header in section of tableView by navigation bar button tapped?如何通过点击导航栏按钮显示和隐藏 tableView 部分的标题?
【发布时间】:2018-04-07 23:59:30
【问题描述】:

使用 Xcode 9.3 版 Swift 开发 iOS 应用程序。

您能告诉我如何通过点击导航栏按钮来显示和隐藏(切换)tableView 部分的标题吗?

  • 搜索栏放置在部分的标题中,使其在滚动 tableview 时始终可见
  • 我想在初始显示中隐藏搜索栏
  • 并且,通过点击导航栏按钮显示和隐藏搜索栏

代码及截图如下。

代码

import UIKit

class TableViewController: UITableViewController, UISearchBarDelegate {

    let array = ["apple", "orange", "melon", "banana", "peach"]
    let searchBar = UISearchBar()

    override func viewDidLoad() {
        super.viewDidLoad()

        let searchButton = UIBarButtonItem(title: "Search", style: UIBarButtonItemStyle.plain, target: self, action: #selector(searchTapped))
        self.navigationItem.leftBarButtonItem = searchButton
    }

    @objc func searchTapped() {
        // If searchBar is hidden, then show a searchBar.

        // If searchBar is shown, then hide a searchBar.

    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        searchBar.delegate = self
        searchBar.placeholder = "Search..."
        searchBar.showsCancelButton = true
        return searchBar
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        searchBar.sizeToFit()
        return searchBar.frame.height
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = array[indexPath.row]
        return cell
    }

}

截图

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:
    var hideSearchBar = false     // Class global var
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
         return hideSearchBar ? 0.1 : searchBarHeight
    }  
    
    @IBAction func search(sender: UIBarButtonItem) {
         hideSearchBar = !hideSearchBar
         tableView.reloadData()
    } 
    

    如果要隐藏 searchBar,请记住将部分标题的高度保持为 0.1。保持高度 0 不起作用。

    【讨论】:

    • 问题解决了,我可以做我想做的事了。非常感谢您的帮助!
    • @TakeYourMark : 随时:)
    【解决方案2】:

    你可以这样做:

    @objc func searchTapped() {
        // If searchBar is hidden, then show a searchBar.
        tableView.tableHeaderView = self.searchBar;
        // If searchBar is shown, then hide a searchBar.
        tableView.tableHeaderView = nil;
        tableView.reloadData();
    }
    

    或者你可以定义:

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    // if you don't need header then return nil or return uiview for header...
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用UITableViewDelegate 方法中的tableView:heightForHeaderInSection: Appledoc。如下

      override viewDidLoad() {
           super.viewDidLoad()
           yourBarButtonTapped = false
      
           //rest of your code
      }
      
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
           return yourBarButtonTapped ? 0.1 : current_height
      }
      
      @IBAction func barButtonTapped() {
           yourBarButtonTapped = true
           yourTableView.reloadData()
      }
      

      【讨论】:

      • 问题解决了,我可以做我想做的了。非常感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多