PickerView加载tableview数据Swift

时间:2015-03-10 14:24:27

标签: xcode uitableview swift uipickerview

目前,我的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])
        }
    }

1 个答案:

答案 0 :(得分:2)

我假设您在与pickerView相同的视图中有一个tableView(如果您正在做什么,则需要为表视图创建一个插座并将其连接到委托和数据源)。我的答案是基于这个假设。

基本上,只需在cellForRowAtIndexPath:方法中添加条件语句即可。一种方法是声明一个变量来保存当前选择的选择器值:

var pickerIdentifier: String?

然后,在pickerView(_: didSelectRow:)方法中,将pickerIdentifier设置为选择的值。

最后,在dequeueReusableCellWithIdentifier方法中写一个条件,用适当的camerondata数组值填充单元格,具体取决于pickerIdentifier的值。

如果将来cameronadata1camerondata2内的值不匹配,您需要为numberofRowsInSection:方法设置另一个条件。< / p>

始终考虑你想做什么并将其分解。如果要根据其他内容更改表行单元格的文本值,则需要转到创建(或出列)表格视图单元格的位置。此外,如果它依赖依赖,则使用条件。如果您的表格查看单元格没有显示任何内容,那么您就无法正确连接表格视图。

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
}
}