PickerView 加载 tableview 数据 Swift [英] PickerView load up tableview data Swift

查看:24
本文介绍了PickerView 加载 tableview 数据 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的 uipicker 有 3 个选择,Cameron、Shaffer 和 Hydril.当我选择 Cameron 时,我希望我的 uitableview 加载camerondata1.如果我选择Shaffer,我希望它加载camerondata2.

Currently, my uipicker has 3 selections, Cameron, Shaffer and Hydril. When I pick Cameron, i want my uitableview to load up camerondata1. If i pick Shaffer, I want it to load up camerondata2.

如何设置我的 UItableview 以根据我的选择器视图上的选择重新调整?我是 Swift 编程的新手.

How do I setup my UItableview to readjust according to the selection made on my picker view? I new to Swift programming.

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

推荐答案

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

I'm assuming you have a tableView within the same view as your pickerView (if that's what you're doing you need to make an outlet for your table view and connect it to the delegate and data source). My answer is based off of this assumption.

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

Basically, just put a conditional statement within your cellForRowAtIndexPath: method. One way you could do this is to declare a variable to hold whichever picker value is currently selected:

var pickerIdentifier:字符串?

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

Then, within your pickerView(_: didSelectRow:) method, set the pickerIdentifier to the selection's value.

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

Finally, write a conditional within your dequeueReusableCellWithIdentifier method to populate the cell with the proper camerondata array values depending on the value of pickerIdentifier.

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

If in the future the number of values within cameronadata1 and camerondata2 don't match, you'll need to set another conditional on your numberofRowsInSection: method.

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

Always think what you want to do and break it down. If you want to change the text values of a table row cell depending on something else, then you want to go to where table view cells are created (or dequeued). Also, if it's dependent on something else then use a conditional. If your table view cells aren't showing anything, then you haven't hooked up your table view properly.

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

这篇关于PickerView 加载 tableview 数据 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆