gpt4 book ai didi

ios - 如何避免此处搜索栏和表格 View 之间的额外间隙并在它们之间放置分隔符?

转载 作者:行者123 更新时间:2023-11-28 05:53:23 25 4
gpt4 key购买 nike

我的代码产生以下结果:Screenshot 1 , Screenshot 2

但是我应该得到这些: Requirement

我试过改正。但是我惨败了,我不明白我在这里做错了什么。请帮我找出这里可能出了什么问题以及我应该如何纠正它。提前致谢。

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.
}



}

在搜索栏附加到 TableView 后,它应该看起来是:like this而不是 like this

最佳答案

viewDidLoad() 中添加以下代码:

if #available(iOS 11, *) {
self.tableView.contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}

在设置您的 SearchController 后添加以下行:

searchController.hidesNavigationBarDuringPresentation = false

关于ios - 如何避免此处搜索栏和表格 View 之间的额外间隙并在它们之间放置分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52135318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com