PickerView 加载 tableview 数据 Swift
PickerView load up tableview data Swift
目前我的uipicker有3个选择,Cameron、Shaffer和Hydril。当我选择 Cameron 时,我希望我的 uitableview 加载 camerondata1。如果我选择 Shaffer,我希望它加载 camerondata2。
如何设置我的 UItableview 以根据在我的选择器视图上所做的选择重新调整?我是 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 相同的视图中有一个 tableView(如果这是您正在做的,您需要为您的 table 视图创建一个插座并连接它委托和数据源)。我的回答是基于这个假设。
基本上,只需在 cellForRowAtIndexPath:
方法中放置一个条件语句。一种方法是声明一个变量来保存当前选择的选择器值:
var pickerIdentifier: String?
然后,在您的 pickerView(_: didSelectRow:)
方法中,将 pickerIdentifier
设置为选择的值。
最后,根据 pickerIdentifier
的值,在您的 dequeueReusableCellWithIdentifier
方法中编写一个条件,以使用适当的 camerondata 数组值填充单元格。
如果将来 cameronadata1
和 camerondata2
中的值数量不匹配,您需要在 numberofRowsInSection:
方法中设置另一个条件。
总是想着你想做的事,然后把它分解。如果您想根据其他内容更改 table 行单元格的文本值,那么您想转到创建(或出队)table 视图单元格的位置。另外,如果它 dependent 依赖于其他东西,那么使用条件。如果您的 table 视图单元格没有显示任何内容,那么您没有正确连接 table 视图。
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
}
}
目前我的uipicker有3个选择,Cameron、Shaffer和Hydril。当我选择 Cameron 时,我希望我的 uitableview 加载 camerondata1。如果我选择 Shaffer,我希望它加载 camerondata2。
如何设置我的 UItableview 以根据在我的选择器视图上所做的选择重新调整?我是 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 相同的视图中有一个 tableView(如果这是您正在做的,您需要为您的 table 视图创建一个插座并连接它委托和数据源)。我的回答是基于这个假设。
基本上,只需在 cellForRowAtIndexPath:
方法中放置一个条件语句。一种方法是声明一个变量来保存当前选择的选择器值:
var pickerIdentifier: String?
然后,在您的 pickerView(_: didSelectRow:)
方法中,将 pickerIdentifier
设置为选择的值。
最后,根据 pickerIdentifier
的值,在您的 dequeueReusableCellWithIdentifier
方法中编写一个条件,以使用适当的 camerondata 数组值填充单元格。
如果将来 cameronadata1
和 camerondata2
中的值数量不匹配,您需要在 numberofRowsInSection:
方法中设置另一个条件。
总是想着你想做的事,然后把它分解。如果您想根据其他内容更改 table 行单元格的文本值,那么您想转到创建(或出队)table 视图单元格的位置。另外,如果它 dependent 依赖于其他东西,那么使用条件。如果您的 table 视图单元格没有显示任何内容,那么您没有正确连接 table 视图。
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
}
}