目前我的uipicker有3个选择,Cameron、Shaffer和Hydril。当我选择 Cameron 时,我希望我的 uitableview 加载 camerondata1。如果我选择 Shaffer,我希望它加载 camerondata2。
如何设置我的 UItableview 以根据在我的选择器 View 上所做的选择重新调整?我是 Swift 编程的新手。
class Picker2: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate
{
var activeTextField:UITextField?
let textCellIdentifier = "TextCell"
let camerondata1 = ["Working Pressure (psi): 5,000", "Fluid to Close (gal): 1.69",]
let camerondata2 = ["Working Pressure (psi): 10,000", "Fluid to Close (gal): 2.69",]
@IBOutlet var pickerView1: UIPickerView!
@IBOutlet var textField1: UITextField!
var brand = ["Cameron","Shaffer", "Hydril"]
override func viewDidLoad() {
super.viewDidLoad()
pickerView1 = UIPickerView()
pickerView1.tag = 0
pickerView1.delegate = self
self.view.addSubview(textField1)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return brand.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerView.tag == 0 {
return brand[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
textField1.text = brand[row]
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return camerondata1.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell.textLabel?.text = camerondata2[row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
println(camerondata1[row])
}
}
请您参考如下方法:
我假设您在与 pickerView 相同的 View 中有一个 tableView(如果这是您正在做的,您需要为您的 TableView 创建一个导出并将其连接到委托(delegate)和数据源)。我的回答是基于这个假设。
基本上,只需在您的 cellForRowAtIndexPath: 方法中放置一个条件语句。您可以这样做的一种方法是声明一个变量来保存当前选择的任何选择器值:
var pickerIdentifier: String?
然后,在您的 pickerView(_: didSelectRow:) 方法中,将 pickerIdentifier 设置为选择的值。
最后,在您的 dequeueReusableCellWithIdentifier 方法中编写一个条件,以根据 pickerIdentifier 的值使用正确的 camerondata 数组值填充单元格。
如果将来 cameronadata1 和 camerondata2 中的值数量不匹配,您需要在 numberofRowsInSection 上设置另一个条件: 方法。
总是想想你想做什么,然后分解它。如果您想根据其他内容更改表格行单元格的文本值,那么您想转到创建(或出列)表格 View 单元格的位置。此外,如果它依赖 于其他事物,则使用条件。如果您的表格 View 单元格没有显示任何内容,那么您没有正确连接表格 View 。
class ViewController: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate {
var pickerIdentifier: String?
let textCellIdentifier = "TextCell"
let camerondata1 = ["Working Pressure (psi): 5,000", "Fluid to Close (gal): 1.69",]
let camerondata2 = ["Working Pressure (psi): 10,000", "Fluid to Close (gal): 2.69",]
@IBOutlet var pickerView1: UIPickerView!
@IBOutlet weak var tableView: UITableView!
var brand = ["Cameron","Shaffer", "Hydril"]
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return brand.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return brand[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerIdentifier = brand[row]
tableView.reloadData()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pickerIdentifier == "Cameron" ? camerondata1.count : camerondata2.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as UITableViewCell
if pickerIdentifier == "Cameron" {
cell.textLabel!.text = camerondata1[indexPath.row]
} else {
cell.textLabel!.text = camerondata2[indexPath.row]
}
return cell
}
}
