Skip to content

Instantly share code, notes, and snippets.

@michelson
Last active January 1, 2016 17:18
Show Gist options
  • Save michelson/8175905 to your computer and use it in GitHub Desktop.
Save michelson/8175905 to your computer and use it in GitHub Desktop.
Formotion Picker with search bar
module Formotion
module RowType
class PickerWithSearchRow < PickerRow
include RowType::ItemsMapper
def after_build(cell)
super
@items_for_search = items
keyboardDoneButtonView = UIToolbar.new
keyboardDoneButtonView.barStyle = UIBarStyleBlack
keyboardDoneButtonView.translucent = true
keyboardDoneButtonView.tintColor = nil
keyboardDoneButtonView.sizeToFit
@txtSearch = UISearchBar.alloc.initWithFrame([[15, 7], [240, 31]])
@txtSearch.tag = 1
@txtSearch.barStyle = UIBarStyleBlackTranslucent
@txtSearch.backgroundColor = UIColor.clearColor
@txtSearch.delegate = self
@txtSearch.userInteractionEnabled = true
@copyListOfItems = []
doneButton = UIBarButtonItem.alloc.initWithTitle("Done", style:UIBarButtonItemStyleDone, target:self, action: 'picker_done_clicked')
spacer1 = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemFlexibleSpace, target:self, action: nil)
spacer = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemFlexibleSpace, target:self, action: nil)
keyboardDoneButtonView.setItems([spacer, spacer1, doneButton])
keyboardDoneButtonView.addSubview(@txtSearch)
row.text_field.inputAccessoryView = keyboardDoneButtonView
end
def picker_done_clicked
# 'jump' to next input field if one exists
#if row.next_row && row.next_row.text_field
#row.next_row.text_field.becomeFirstResponder
#else
# just hide the keyboard...
row.text_field.resignFirstResponder
#end
end
def searchBarTextDidBeginEditing(theSearchBar)
@searching = false
letUserSelectRow = false
end
def searchBar(theSearchBar, textDidChange:searchText)
@copyListOfItems.removeAllObjects
if searchText.length > 0
@searching = true
letUserSelectRow = true
self.searchTableView(searchText.downcase)
else
@searching = false
letUserSelectRow = false
end
picker.reloadAllComponents
end
def searchBarSearchButtonClicked(searchBar)
self.btnDoneClick
end
def searchTableView(searchText)
p searchText
@items_for_search.each do |t, v|
if v.downcase.include?(searchText)
@copyListOfItems << v
end
end
p @copyListOfItems
picker.reloadAllComponents
end
def textFieldShouldReturn(textField)
p "RESIGN"
textField.resignFirstResponder
true
end
def btnDoneClick
if @searching
p "SEARCHING"
if @copyListOfItems.count > 0
p "1"
strSelectedValue = @copyListOfItems[picker.selectedRowInComponent(0)]
#p strSelectedValue
self.row.items = @copyListOfItems
select_picker_value(@copyListOfItems.first)
else
p "2"
# if not results , then set the original list
p @items_for_search
self.row.items = @items_for_search
end
else
# maybe pre select some option ??
end
row.text_field.resignFirstResponder # hides keyboard
row.text_field.becomeFirstResponder # show picker
end
def numberOfComponentsInPickerView(pickerView)
1
end
def pickerView(pickerView, numberOfRowsInComponent:component)
if @searching
@copyListOfItems.count
else
self.items.count
end
end
def pickerView(pickerView, titleForRow:row, forComponent:component)
if @searching
@copyListOfItems[row]
else
item_names[row]
end
end
def item_names
self.items.map{ |name, value| name }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment