【问题标题】:PickerView load up tableview data SwiftPickerView 加载表格视图数据 Swift
【发布时间】:2015-03-10 14:24:27
【问题描述】:

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

【问题讨论】:

    标签: xcode uitableview swift uipickerview


    【解决方案1】:

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

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

    var pickerIdentifier: String?

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

    最后,在dequeueReusableCellWithIdentifier 方法中编写一个条件,根据pickerIdentifier 的值使用正确的camerondata 数组值填充单元格。

    如果将来cameronadata1 和camerondata2 中的值数量不匹配,您需要在numberofRowsInSection: 方法上设置另一个条件。

    总是想你想做什么,然后把它分解。如果要根据其他内容更改表格行单元格的文本值,则需要转到创建(或出列)表格视图单元格的位置。此外,如果它依赖其他东西,那么使用条件。如果您的表格视图单元格没有显示任何内容,则说明您没有正确连接表格视图。

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

    【讨论】:

    • 你把interface builder中的tableView连接到view controller的数据源和delegate了吗?
    • 是的,tableview 连接到数据源和委托。我收到一个 '(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'reloadData' 错误。在 tableView.reloadData()。
    • 很抱歉……你是对的。我确实忘记连接 uitableview。
    猜你喜欢
    • 1970-01-01
    • 2016-05-20
    • 2019-07-08
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多