UIPickerView 和 UISearchController 不显示数据

Posted

技术标签:

【中文标题】UIPickerView 和 UISearchController 不显示数据【英文标题】:UIPickerView and UISearchController not displaying data 【发布时间】:2017-01-16 04:14:55 【问题描述】:

我目前遇到了一些麻烦,希望其中一位专家能够评估并回答我的问题。我制作了一个 UIPickerView 和一个 UISearchController(它在 UITableViewController 中),它们都没有显示我制作的数组中的数据。这是一个学校项目,直到星期二才到期,但我希望我能修复这些小(希望是错误)。我将向您展示我的 UIPickerView 和 UISearchController 的 ViewController。 所以这里是 UIPickerView :


import UIKit

class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate 

@IBOutlet weak var txtTest: UITextField!

@IBOutlet weak var Picker1: ThePickerView!
var sellInvestments = ["Airbnb", "CloudFlare", "GitHub", "slack", "Snapchat", "Uber"]

override func viewDidLoad() 
    super.viewDidLoad()

// Do any additional setup after loading the view.

    self.Picker1.delegate = self
    self.Picker1.dataSource = self
    [self.Picker1.reloadAllComponents]


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

func pickerView(_pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 
    return sellInvestments[row]

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return sellInvestments.count

public func numberOfComponents(in pickerView: UIPickerView) -> Int 

    return 1

现在这里是 UITableViewController(用于 UISearch)

import UIKit

class NewTableViewController: UITableViewController, UISearchResultsUpdating 

let searchInvestments = ["Berlark Company", "Snapchat", "IBM", "Twitter", "Cornerstone on Demand", "Aukvern Capital", "Serene Technologies", "Viacom Industries", "Suvertec", "HIppit", "Avigti Technologies", "Avaret", "Sivlot", "Zebra Sci Automation", "Google", "Apple", "Facebook", "Gradience Imaging", "Vitris", "Voxtrat", "WhattsApp", "Apphat", "Nividia", "Kik", "Cyber Dust", "Turing Technologies", "Sobel Imaging Systems", "Yavid", "Tensor Automation", "Vistapint", "LinkedIn", "Yahoo", "Yelp", "TwitchTv", "OculusRift", "Lg", "Intel", "Amazon", "Sony", "Samsung", "Microsoft", "HP", "Vencore", "AT&T", "Verizon", "Dell", "MicroTech", "Flickr"]
var filteredInvestments = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad() 
    super.viewDidLoad()
    self.resultSearchController = UISearchController(searchResultsController: nil)
    self.resultSearchController.searchResultsUpdater = self
    self.resultSearchController.dimsBackgroundDuringPresentation = false
    self.resultSearchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = self.resultSearchController.searchBar
    self.tableView.reloadData()



    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()


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


// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int 
    // #warning Incomplete implementation, return the number of sections
    return 1


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    // #warning Incomplete implementation, return the number of rows
    if self.resultSearchController.isActive
    
       return self.filteredInvestments.count
    
    else 
        return self.searchInvestments.count

    




override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell?

    if self.resultSearchController.isActive
    
        cell!.textLabel?.text = self.filteredInvestments[indexPath.row]
    
    else 
        cell!.textLabel?.text = self.searchInvestments[indexPath.row]
    

    return cell!


func updateSearchResults(for searchController: UISearchController) 
    self.filteredInvestments.removeAll(keepingCapacity: false)
   let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
  let array  = (self.filteredInvestments as NSArray).filtered(using: searchPredicate)
    self.filteredInvestments = array as! [String]
    self.tableView.reloadData()




好的,这样就有了 PickerView 和 Search 视图的代码。 *没有编译错误,应用程序运行。我将它连接到我的 iPhone 5c 并且能够很好地运行该应用程序。

以下是应用运行的一些图片。 (有未填充的视图)

这里是图片的链接(因为我没有 10 的声望) http://imgur.com/a/0Gp4P - PickerView http://imgur.com/a/JiUFe - UISearch

非常感谢你能回答这个问题,因为我浏览了网络,大多数时候几乎没有帮助,而那些有帮助的人却什么也没做。总而言之,当我运行它时,我放入数组中的字符串都没有显示出来。

非常感谢你们!

【问题讨论】:

【参考方案1】:

对于您的 UIPickerView,您忘记了功能:

 func pickerView(_ pickerView: UIPickerView, viewForRow row: Int,     forComponent component: Int, reusing view: UIView?) -> UIView 

 let pickerLabel = UILabel() //Create label
 pickerLabel.textColor = UIColor.black //Create text color
 pickerLabel.text = pickerData[row] //Make text for row the one in array
 pickerLabel.font = UIFont(name: "Helvetica-Light", size: 20) //Text font
 pickerLabel.textAlignment = NSTextAlignment.center //Alignment
 return pickerLabel

至于搜索,使用与上面类似的功能,只是没有选择器视图,它将用于搜索视图。自动完成应该有助于找到它。

【讨论】:

博兰会非常感谢!做到了,现在我将在 UISeach 上尝试一下 有谁知道搜索的相同功能是什么?因为我看不到一个自动填充? 没问题。我会尽快用该信息更新答案。现在正在努力。 如果有人可以通过告诉我搜索的功能是什么来增加一些帮助,那将是甜蜜的,谢谢。 如果你能提供你的项目,我可以弄清楚这个问题的原因是与缺少的功能有关还是与其他东西有关。我也会继续寻找。

以上是关于UIPickerView 和 UISearchController 不显示数据的主要内容,如果未能解决你的问题,请参考以下文章

UIPickerView 简单操作和实际应用

为 UIPickerView 创建委托和数据源

RxSwift 和 UIPickerView

UIPickerView 和 CoreData 数组

显示和隐藏 UIPickerview 时出错

强制 UIPickerView 重新加载数据