The UIPickerView is a very simple UIView that presents a list of choices to you, sorted either in the traditional manner (based on the user’s previous selections) or in alphabetical order. It’s very useful to use in many situations, but has a couple of quirks and issues which make it difficult to use. I will explain how to make it easier and how to make it the best you can.
UIPickerView is a view that allows you to pick (or randomly select) elements out of a list. However, UIPickerView is a little bit annoying when it comes to basic UIPickerView programming. You need to implement every single view property and method and handle every single event separately to get UIPickerView to do what you want. This is where the UIPickerInspector class comes in. It provides a lot of utilities that make UIPickerView programming much easier.
Most applications have to deal with the UIPickerView at some point, but the task can be quite tricky. UIPickerView, a sub-class of AViewController, provides an array of cells that can be sorted by any of its attributes.
PickL
PickL is an elegant manager that allows you to easily work with UIPickerView. You no longer need to implement the logic for UIPickerViewDataSource and UIPickerViewDelegate yourself, it is already done under the hood.
Use
PickL is the main object, it frees you from the direct management of the delegate and the data source.
PickL(voterView : voterView)
Because of some limitations of UIPickerView, you cannot maintain a view and string items simultaneously in the same picker view. Therefore, you must point to the adapter: StringAdaptor or ViewAdaptor ! electric_plug](https://github.githubassets.com/images/icons/emoji/unicode/1f50c.png =20×20).
Parts/elements of the series
A little terminology – the image below explains everything you need for further explanation.
Let’s dive into the heart of the matter:
- You have several options for creating a row element.
Displayable string. To facilitate the initialization of string elements, there is an extension for NSAttributedString and String :
let rowItem = NSAttributedString(string : Nikita, attributes : [NSAttributedStringKey.foregroundColor : UIColor.red])
Let rowItems : [RowStringItemProtocol]
or direct inheritance from RowStringItemProtocol :
/// In this case, you have `didSelectHandler`.
let rowItem1 = RowStringItem(title : Nikita)
let rowItem2 = RowAttributedStringItem(attributedTitle : NSAttributedString(string : Artem))
CustomRowItem class: RowStringItemProtocol {
let name : The line is
init(name : String) {
self.name = name
}
func pickerView(_ pickerView : UIPickerView, titleForRow row : Int, forComponent component : Int) -> String ? {
return name
}
}
And a similar approach for representable:
let rowItem1 = UIView(title : Nikita)
let rowItem2 = UIView(title : Artem)
CustomRowItem class: RowViewItemProtocol {
pickerView(_ pickerView : UIPickerView, viewForRow row : Int, forComponent component : Int, view reuse : UIView ?) -> UIView {return UIView() /// return custom view}
- Then create a component element with these string elements.
Let componentItem = ComponentItem(rowItems : [rowItem1, rowItem2])
- Finally, you need to install this component.
Let pickL = PickL(pickerView : pickerView)
pickL.components = [componentItem].
A little more about the part.
Component products offer a variety of approaches from which to choose:
- selectRow(_row : Int, animated : Bool)
- selectNextRow(animated : Bool, checkIndexOutOfRange : Bool, isLoopEnabled : Bool)
- selectPreviousRow(animated : Bool, checkIndexOutOfRange : Bool, isLoopEnabled : Bool)
- selectFirstRow(animated : Bool)
- selectLastRow(animated : Bool)
Line selection definition
The user has several options to determine the line selection:
- Any RowItem inherited from RowStringItem, RowAttributedStringItem or directly from RowItem has a callback function with the index of the string and the index of the component passed in – useful if you want to track the selection of a particular item :
rowItem.didSelectHandler = { rowIndex, componentIndex in
// place to display
}
- If you do not want to monitor changes to a particular line item, you can also monitor the line selection directly from the Monitor component :
componentItem.didSelectRowHandler = { componentItem, rowIndex, rowItem in
print( )
}
or directly get the element of the selected row: componentItem.selectedRowItem
- Finally, the user has the option to define the selection of all lines of each component in a single callback:
pickL.selectedRowsHandler { rowIndex1, rowIndex2, … and
print((rowIndex1), (rowIndex2))
}
or use an array with all selected strings:
print(pickL.selectedRows) // [0, 1].
Score: This callback function has up to 7 transferable parameters (string indices).
Even if you have more than 7 stones, there is a common solution:
pickL.selectedRowsArrayHandler { selectedRows in
print(selectedRows)
}
Setting
Depo
Depo is a universal dependency manager that integrates Carthage, SPM and CocoaPods and provides a common user interface for all of them.
To install PickL through Carthage with Depo, you need to add it to your depofile:
Car:
– View: github
-User-ID: rosberry/PickL
To install PickL with CocoaPods
Carthago
Create a cartfile specifying the framework and run the carthage update. Follow the instructions to add the framework to your project.
github rosberry/PickL
CocoaPods
You can use CocoaPods to install PickL by adding it to your podfile:
Platform :ios, ‘12.0’
use_frameworks !
PickL sleeve
Manual
Drag the Sources folder from the latest version into your project.
GitHub
https://github.com/rosberry/PickLFirst, learn how to create multiple UIPickerViews in one view controller, and then how to create UIPickerViews with custom views. Now learn how to create a simple manager to easily deal with your UIPickerViews.. Read more about uipickerview border color and let us know what you think.
Related Tags:
uipickerview get selected rowcenter uipickerviewuipickerview modaluipickerview multiple components swiftuipickerview border coloruipickerview delegate,People also search for,Privacy settings,How Search works,uipickerview get selected row,center uipickerview,uipickerview modal,uipickerview multiple components swift,uipickerview border color,uipickerview delegate,dismiss uipickerview swift,numberofrowsincomponent