xcode - PickerView 加载 tableview 数据 Swift

标签 xcode uitableview swift uipickerview

目前,我的 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: 字符串?

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

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

如果将来 cameronadata1camerondata2 中的值数量不匹配,您需要在 numberofRowsInSection: 方法。

始终思考您想做什么并将其分解。如果您想根据其他内容更改表行单元格的文本值,那么您需要转到 TableView 单元格创建(或出列)的位置。另外,如果它依赖其他东西,那么就使用条件。如果您的表格 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
}
}

关于xcode - PickerView 加载 tableview 数据 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966278/

相关文章:

iphone - 如何解决 uitableview 中对象的释放问题?

ios - iOS 表/ Collection View 中的单元格间布局逻辑

ios - 在 didSelectRowAtIndexPath 中动画 NSLayoutConstraint 不起作用

ios - Objective C 应用程序中的 “Swift Language Version” 错误

ios - 在没有可见源代码的情况下分发用于测试的 iPhone 应用程序

ios - 这是 UITableViewCellAccessoryDe​​tailDisclosureButton 的正常行为吗

swift - 模块作为 Xcode 项目中的独立框架

ios - 使用 NSStringCompareOptions 在 iOS 中对负数字符串进行排序

ios - 获取顶部约束的引用

ios - 类型为 Void 的 appendAttributedString 返回值