我目前遇到了一些麻烦,并希望其中一位专家能够评估并希望回答我的问题。我已经制作了一个UIPickerView和一个UISearchController(它位于UITableViewController中),并且两者都没有显示我制作的数组中的数据。这是一个学校项目,直到星期二才到期,但我希望我能解决这些小问题(希望是错误)。我将向您展示UIPickerView和UISearchController的ViewControllers。 所以这是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
非常感谢你,如果你能够回答这个问题,因为我已经浏览了网络,而且大部分时间都没有帮助,而那些得到帮助的人却没有做任何事情。总而言之,当我运行它时,我放入数组的字符串都没有显示出来。
非常感谢你们!
答案 0 :(得分:0)
对于你的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
}
对于搜索,使用与上面类似的功能,只是没有选择器视图,它将用于搜索视图。自动填充应该有助于找到它。