swift - 运行我的应用程序时出现两个搜索栏,但情节提要上只显示一个
问题描述
我目前正在构建一个表格视图,我注意到在填充我的表格视图后,我得到了两个搜索栏字段。我的初衷是只出现一个搜索栏字段(显示在我的界面上)我似乎无法弄清楚这是怎么回事。当我尝试从界面中删除搜索栏时,我的表格视图将不会加载和填充数据。
这是界面故事板的屏幕截图:
这是我运行应用程序时的屏幕截图:
这是一些代码,包括我的 viewDidLoad: 编辑:添加 TableView 代码
class AddHarvestPlanViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var AddHarvestPlanPluCodeTable: UITableView!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
findPluCodeParents(searchTextField: "apple")
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Find your Commodity"
navigationItem.searchController = searchController
definesPresentationContext = true
AddHarvestPlanPluCodeTable.delegate = self
AddHarvestPlanPluCodeTable.dataSource = self
AddHarvestPlanPluCodeTable.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = harvestCommodities.count
return count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AddPluCodeCustomCell", for: indexPath) as! AddPluCodeCustomCell
do{
let item = harvestCommodities[indexPath.row]
cell.commodity?.text = item.plu_code_commodity
cell.package?.text = "Cases"
return cell
}
}
func findPluCodeParents(searchTextField:String){
let searchText = searchTextField
print("Searching for....",searchTextField)
let aggriEndpoint: String = "https://xxxx.xxxx.com/xxxx/xxxxx"
let url = URL(string:aggriEndpoint)
var urlRequest = URLRequest(url:url!)
urlRequest.httpMethod = "GET"
let session = URLSession.shared
print("running task")
let task = session.dataTask(with: urlRequest, completionHandler: {data,response,error -> Void in
do{
print("All finiished")
print(data)
if let json = try JSONSerialization.jsonObject(with: data!) as? [[String:Any?]]{
// print(json)
for item in json{
print(item as? [String:Any?])
var active = item["active"] as? Bool
let cases_per_week = (item["cases_per_week"] as! NSString).doubleValue
var cases_per_palette = item["cases_per_pallette"] as? Int
var lbs_per_week = (item["lbs_per_week"] as! NSString).doubleValue
var cases_per_week_avg = item["cases_per_week_avg"] as? Int
var pounds_per_case = item["pounds_per_case"] as? Int
var repeat_harvest = item["repeat_harvest"] as? Bool
var harvest_week_count = item["harvest_week_count"] as? Int
var plant_days = item["plant_days"] as? Int
var pounds_per_acre = item["pounds_per_acre"] as? Int
var options = item["options"] as? Int
var plu_code_variety = item["plu_code_variety"] as? String
var plu_code_commodity = item["plu_code_commodity"] as? String
var id = item["id"] as? Int
var plu_code = item["plu_code"] as? Int
var acres = item["acres"] as? Int
self.harvestCommodities.append(PluCode(id: id!, commodity: plu_code_commodity!, cases_per_week_avg: cases_per_week_avg!, repeat_harvest: repeat_harvest!, cases_per_week: cases_per_week, lbs_per_week: lbs_per_week, acres: acres!, plu_code: plu_code!, active: active!, options: options!, plant_days: plant_days!, plu_code_commodity: plu_code_commodity!, plu_code_variety: plu_code_variety!, cases_per_palette: cases_per_palette!, harvest_week_count: harvest_week_count!, pounds_per_acre: pounds_per_acre!))
}
}
DispatchQueue.main.async {
self.AddHarvestPlanPluCodeTable.reloadData()
}
} catch let error{
print("error")
}
})
task.resume()
}
解决方案
要么删除创建上部搜索栏的代码(“let searchController = UISearchController(searchResultsController: nil)”),然后将 IBOutlet 连接到您在 swift 文件的接口上创建的那个,或者删除故事板并使用您在代码中创建的故事板,并将结果连接到表格视图
推荐阅读
- angular - Angular 中的二进制输入验证器(TypeScript)
- ssms - SQL Server Management Studio 安装期间出现致命错误 0x80070643
- robotframework - 如何将查询结果打印到控制台 | 机器人框架
- php - PHP for 循环仅每两行有效
- statistics - 如何在 Julia 中操作数据框时使用条件
- reactjs - 使用带有 React Js 的“Unstated”从 API 端点获取数据
- authentication - 使用 azure 广告身份验证时抛出异常 AADSTS50011
- mysql - phpmyadmin 中使用少量 INNER JOIN 的 504 超时网关问题
- ios - 找不到目标“arm64-apple-ios-simulator”的模块“Alamofire”;找到:x86_64-apple-ios-simulator,x86_64
- azure - 微软在 azure devops 中托管的 ubuntu 代理中是否允许使用端口 9000