Array data is not visible on picker view - swift

I have array data to show on UIPickerView, but data is not showing on picker view. My code:
#IBOutlet var txt: UITextField!
let PickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
Json()
txt.inputView = PickerView
PickerView.delegate = self
PickerView.dataSource = self
// Do any additional setup after loading the view.
}
func Json()
{
{ (response, error) in
if response != nil
{
let Value = response!["data"] as! [[String:Any]]
for dic in dataDict
{
if let job = dic["Jobs"] as? String
{
print(job)
self.array.append(job)
print(self.array)
}
}
}
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txt.text = array[row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}
Why is this happening, is there any way to fix this?

txt.inputView = PickerView
PickerView.delegate = self
PickerView.dataSource = self
Json()
please reload picker.reloadAllComponent() in response

Related

How to clip this corners(on the screenshot) in UIPickerView?

Yes! I tried use "clip to bounds" bun in doesnt work, and i dont now why. Notice to gray areas on the sides
enter image description here
try this..
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var txtPicker: UITextField!
var testPickerView : UIPickerView!
var testArr = ["One","Two","Three","Four","Five"]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
testPickerView = UIPickerView()
testPickerView.dataSource = self
testPickerView.delegate = self
testPickerView.backgroundColor = .green
testPickerView.layer.cornerRadius = 30.0
testPickerView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
testPickerView.clipsToBounds = true
txtPicker.inputAccessoryView = testPickerView
}
// MARK: pickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return testArr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
txtPicker.resignFirstResponder()
return testArr[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txtPicker.text = testArr[row]
}
}

UIPickerView row text is hidden

I believe I have followed the steps to implement a UIPickerView to display an array of strings. For some reason, the text from titleForRow is hidden. I seems like the picker view is getting the array count because I have the ability to scroll. I just can't get the list if names. I am using UIStoryboard and all outlets are connected. Any help would be appreciated.
extension SongPlayerViewController: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return hertzArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return hertzArray[row]
}
//when user selects row
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}
class SongPlayerViewController: UIViewController {
let hertzArray = ["432Hz", "528Hz", "174Hz", "396Hz", "417Hz", "639Hz", "741Hz", "852Hz"]
#IBOutlet weak var pickerView: UIPickerView!
#IBOutlet weak var toolBar: UIToolbar!
//when view did load
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
}
}
I don't think there is a problem with the code you've posted. I put it into a playground with a little bit of setup code and it works. You should check your outlets in the nib file and check at runtime to ensure that the pickerView property is being assigned. As suggested in comments, check the dimensions of the picker using the view debugging tools
(https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html)
Here's the Playground I was working with:
import UIKit
import PlaygroundSupport
class SongPlayerViewController: UIViewController {
let hertzArray = ["432Hz", "528Hz", "174Hz", "396Hz", "417Hz", "639Hz", "741Hz", "852Hz"]
#IBOutlet weak var pickerView: UIPickerView!
override func loadView() {
let pickerView = UIPickerView()
self.pickerView = pickerView
view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
view.addSubview(pickerView)
pickerView.translatesAutoresizingMaskIntoConstraints = false
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
}
}
extension SongPlayerViewController: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return hertzArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return hertzArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}
let songController = SongPlayerViewController()
PlaygroundSupport.PlaygroundPage.current.liveView = songController

PickerView show nothing in text field input

When I tap on the textField, the input area appear, but there's only 1 row, with blank content. (I expect 3 rows: a,b and c)
I changed the returned value in numberOfRowsInComponent and titleForRow functions to constants, but get the same result (1 blank row). Maybe, these 2 functions never be called.
Please help me!
//In ViewDidLoad Function (ViewController.swift):
let myPickerView: MyPickerView = MyPickerView(arrData: ["a","b","c"]){ selected in
print(selected)
}
textField.inputView = myPickerView.picker
//In MyPickerView.swift
class MyPickerView: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
var arrData: Array<String>
var picker: UIPickerView
var action: (_ selected: String) -> Void
public init(
arrData: Array<String>,
action: #escaping (_ selected: String) -> Void
) {
self.arrData = arrData
self.action = action
self.picker = UIPickerView()
super.init()
self.picker.delegate = self
self.picker.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.arrData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.arrData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.action(self.arrData[row])
}
}
I found the problem. When I changed myPickerView to global variable (put outside of ViewDidLoad), everything fined.
var myPickerView: MyPickerView?
//In ViewDidload
myPickerView = MyPickerView(arrData: ["a","b","c"]){ selected in
print(selected)
}

Picker view is not showing array data

var Months = ["A","B","C","D"]
var pickerview = UIPickerView()
#IBOutlet var txt_Month: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
pickerview.isHidden = true
pickerview.dataSource = self
pickerview.delegate = self
txt_Month.inputView = pickerview
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Months.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
txt_Month.text = Months[row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return Months[row]
}
maybe it's because pickerview is hidden?
Set the pickerview is unhide.
pickerview.isHidden = false

Cannot convert return expression of type '[String]' to return type 'String?' / UIPickerView

I'm using a UIPickerView and its giving me the error (Cannot convert return expression of type '[String]' to return type 'String?' / UIPickerView). Here is my code.
// where the picker view is set up.
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let threeByThreeArray = ["OLL", "PLL"]
#IBOutlet weak var pickerViewOutlet: UIPickerView!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let row = pickerView.selectedRow(inComponent: 0)
print("this is the pickerView\(row)")
switch row {
case 0:
return threeByThreeArray.count
default:
return cubesToWorkWith.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch row {
case 0:
return threeByThreeArray[row]
default:
return getArrayForRow(row: row)
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
cubeSelected = Int16(row)
}
func getArrayForRow(row: Int) -> [String] {
switch row {
case 0:
return threeByThreeArray
default:
return cubesToWorkWith
}
}
}
}
and I get the error at the switch case inside the titleForRow at "return getArrayForRow(row: row)"
thanks for any help in advance!!!!
so you are missing a few things:
first, you need to set delegate ad Datasource to self
and in the titleForRow:
you need to return a string so your:
func getArrayForRow(row: Int) -> [String]
need to be like:
func getArrayForRow(row: Int) -> String
here is my offer:
class ViewController: UIViewController ,UIPickerViewDelegate,UIPickerViewDataSource{
// where the picker view is set up.
let cubesToWorkWith = ["3X3", "2X2", "4X4", "5X5", "6X6", "7X7", "Skewb", "Square-One"]
let threeByThreeArray = ["OLL", "PLL"]
#IBOutlet weak var pickerViewOutlet: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.pickerViewOutlet.dataSource = self
self.pickerViewOutlet.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let row = pickerView.selectedRow(inComponent: 0)
print("this is the pickerView\(row)")
switch row {
case 0:
return threeByThreeArray.count
default:
return cubesToWorkWith.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch row {
case 0:
return threeByThreeArray[row] as String
default:
return getArrayForRow(row: row) as String
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// cubeSelected = Int16(row)
}
func getArrayForRow(row: Int) -> String {
switch row {
case 0:
return threeByThreeArray[row]
default:
return cubesToWorkWith[row]
}
}
}