Ios 如何避免搜索栏和tableview之间的额外间隙,并在两者之间设置分隔符?

Ios 如何避免搜索栏和tableview之间的额外间隙,并在两者之间设置分隔符?,ios,swift,xcode,Ios,Swift,Xcode,我的代码生成以下结果: 但是我应该得到这些: 我试着纠正同样的错误。然而,我失败得很惨,我不明白我在这里做错了什么。请帮助我找出这里可能存在的问题,以及我应该如何纠正它。提前谢谢 import UIKit class SelectCountryViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating { struct CellStruct { var

我的代码生成以下结果:

但是我应该得到这些:

我试着纠正同样的错误。然而,我失败得很惨,我不明白我在这里做错了什么。请帮助我找出这里可能存在的问题,以及我应该如何纠正它。提前谢谢

import UIKit

class SelectCountryViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {

    struct CellStruct
    {
        var countryName : String
        var countryFlag : String
        var countryDialCode :   String
    }

    var cellDatas = [CellStruct]()

    var filteredCellDatas = [CellStruct]()

    var searchController : UISearchController!
    var resultsController = UITableViewController()




    var searchLoaded = false
    var isSearching = false

    override func viewDidLoad() {
        super.viewDidLoad()

        configureSearchController()

        let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancel))
        navigationItem.leftBarButtonItem = cancelButton
        self.navigationItem.title = "Select Country"

        let searchButton: UIBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(searchButtonAction))
        searchButton.image = UIImage(named: "search")
        self.navigationItem.rightBarButtonItem = searchButton



        guard let path = Bundle.main.path(forResource: "countries", ofType: "json")
            else
        {
            return
        }
        let url = URL(fileURLWithPath: path)
        do
        {
            let data = try Data (contentsOf: url)
            let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
            print(json)
            guard let array = json as? [Any] else { return}
            for info in array {
                guard let userDict = info as? [String: Any] else { return}
                guard let code = userDict["code"] as? String else { print("No code found"); return}
                guard let dialCode = userDict["dial_code"] as? String else { print("No dial code found"); return}
                guard let name = userDict["name"] as? String else { print("No name found"); return}
                print("We have: ", code, dialCode, name)
                cellDatas.append(CellStruct(countryName: name, countryFlag: code, countryDialCode: dialCode))
            }
        }
        catch
        {
            print(error)
        }
    self.automaticallyAdjustsScrollViewInsets = false;


    }
    func configureSearchController()
    {
        self.navigationController?.navigationBar.shadowImage = UIImage()


        resultsController.tableView.delegate = self
        resultsController.tableView.dataSource = self

        self.searchController = UISearchController(searchResultsController: self.resultsController)
        self.tableView.tableHeaderView = self.searchController.searchBar
        self.searchController.searchResultsUpdater = self
        self.searchController.searchBar.layer.borderWidth = 0


        self.searchController.dimsBackgroundDuringPresentation = false
        searchController.searchBar.delegate = self

        searchController.searchBar.layer.borderWidth = 1;
        self.searchController.searchBar.scopeButtonTitles = []
        searchController.searchBar.barTintColor = UIColor.searchBarBackgroundGrey()
        searchController.searchBar.layer.borderColor = UIColor.searchBarBackgroundGrey().cgColor

        for subView in searchController.searchBar.subviews {
            for subViewOne in subView.subviews {
                if subViewOne is UITextField {
                    subViewOne.backgroundColor = UIColor.searchBarTextFieldGrey()
                    break
                }
            }
        }

        self.automaticallyAdjustsScrollViewInsets = false;
        definesPresentationContext = true

    }
    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {

        searchController.searchBar.barTintColor = UIColor.white
        searchController.searchBar.layer.borderColor = UIColor.white.cgColor

        return true
    }
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        self.searchController.searchBar.showsCancelButton = false
        searchController.searchBar.barTintColor = UIColor.searchBarBackgroundGrey()
        searchController.searchBar.layer.borderColor = UIColor.searchBarBackgroundGrey().cgColor


    }

    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.navigationBar.shadowImage = nil
        self.navigationController?.navigationBar.backIndicatorImage = nil
    }

    func updateSearchResults(for searchController: UISearchController) {
        if searchController.searchBar.text! == "" {
            filteredCellDatas = cellDatas
        } else {
            // Filter the results
            filteredCellDatas = cellDatas.filter { $0.countryName.lowercased().contains(searchController.searchBar.text!.lowercased()) }
        }
        resultsController.tableView.reloadData()
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == resultsController.tableView
        {
            isSearching = true
            return filteredCellDatas.count
        }
        else
        {
            isSearching = false
            return cellDatas.count
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
         var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        cell?.separatorInset.left = 15
        if cell == nil {
            cell = UITableViewCell(style: .value1, reuseIdentifier: "Cell")

            cell?.separatorInset.left = 0
        }

        if tableView == resultsController.tableView
        {
            cell?.textLabel?.text = filteredCellDatas[indexPath.row].countryName
            cell?.detailTextLabel?.text = filteredCellDatas[indexPath.row].countryDialCode
            cell?.imageView?.image = UIImage (named: filteredCellDatas[indexPath.row].countryFlag)

        }
        else
        {
            cell?.textLabel?.text = cellDatas[indexPath.row].countryName
            cell?.detailTextLabel?.text = cellDatas[indexPath.row].countryDialCode

            cell?.imageView?.image = UIImage (named: cellDatas[indexPath.row].countryFlag)
        }


        return cell!

    }


    @objc func cancel(){
        navigationController?.popViewController(animated: true)
    }

    @objc func searchButtonAction() {

        searchLoaded = true
        self.tableView.tableHeaderView = searchController.searchBar
        self.searchController.isActive = true
        self.searchController.searchBar.text = ""
        self.navigationItem.rightBarButtonItem = nil

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

将搜索栏附加到表视图后,它应该看起来是:而不是在
viewdiload()中添加以下代码:

在设置
SearchController
后添加以下行:

searchController.hidesNavigationBarDuringPresentation = false

抛出错误:“SelectCountryViewController”类型的值没有成员“contentInsetAdjustmentBehavior”我已尝试self.automaticallyAdjustsScrollViewInsets=false。它没用。谢谢你@iVarun,但现在它已经僵硬了。我甚至看不到搜索栏。@AnnoThra你能添加屏幕截图吗?添加上面的代码后,它看起来如何。检查最后一行。我添加了两个图像。我用这种方法得到了第二个。
searchController.hidesNavigationBarDuringPresentation = false