Skip to content

Instantly share code, notes, and snippets.

@mats-claassen
Created September 12, 2016 14:04
Show Gist options
  • Save mats-claassen/fa8ec5834eeb34659db17a6af71bec57 to your computer and use it in GitHub Desktop.
Save mats-claassen/fa8ec5834eeb34659db17a6af71bec57 to your computer and use it in GitHub Desktop.
A custom picker row for Eureka to select a time (hours, minutes and seconds)
public struct Time: Equatable {
var hours: Int
var minutes: Int
var seconds: Int
}
public func ==(lhs: Time, rhs: Time) -> Bool {
return lhs.hours == rhs.hours && lhs.minutes == rhs.minutes && lhs.seconds == rhs.seconds
}
public class MyPickerCell: PickerCell<Time> {
public required init(style: UITableViewCellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
public override func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 3
}
public override func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return 100
default:
return 60
}
}
public override func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch component {
case 0:
return "\(row) h"
case 1:
return "\(row) m"
default:
return "\(row) s"
}
}
public override func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if let pickerRow = self.row as? MyPickerRow {
var time = pickerRow.value
switch component {
case 0:
time?.hours = row
case 1:
time?.minutes = row
default:
time?.seconds = row
}
pickerRow.value = time
}
}
}
public final class MyPickerRow: Row<Time, MyPickerCell>, RowType {
required public init(tag: String?) {
super.init(tag: tag)
value = Time(hours: 0, minutes: 0, seconds: 0)
}
}
Copy link

ghost commented Oct 5, 2016

Any chance of this being updated to Swift 3.0 syntax?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment