Skip to content

Commit

Permalink
add new row type for UISegmentedControl
Browse files Browse the repository at this point in the history
  • Loading branch information
mordaroso committed Jul 5, 2012
1 parent 692b7a4 commit a68068a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/KitchenSink/app/app_delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def application(application, didFinishLaunchingWithOptions:launchOptions)
type: :email,
auto_correction: :no,
auto_capitalization: :none
}, {
title: "Gender",
key: :gender,
type: :control,
items: ['Female', 'Male']
}, {
title: "Password",
key: :password,
Expand Down
4 changes: 4 additions & 0 deletions lib/formotion/row/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Row < Formotion::Base
# EX 200
# DEFAULT is nil, which is used as the tableView.rowHeight
:rowHeight,
# Array used for control row items
# EX ['free', 'pro']
# DEFAULT is []
:items,
]
PROPERTIES.each {|prop|
attr_accessor prop
Expand Down
24 changes: 24 additions & 0 deletions lib/formotion/row_type/control_row.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Formotion
module RowType
class ControlRow < Base

SLIDER_VIEW_TAG = 1200

def build_cell(cell)
cell.selectionStyle = UITableViewCellSelectionStyleNone
slideView = UISegmentedControl.alloc.initWithItems(row.items || [])
slideView.selectedSegmentIndex = row.items.index(row.value) if row.value
slideView.segmentedControlStyle = UISegmentedControlStyleBar
cell.accessoryView = slideView

slideView.when(UIControlEventValueChanged) do
row.value = row.items[slideView.selectedSegmentIndex]
end

nil
end


end
end
end
37 changes: 37 additions & 0 deletions spec/row_type/control_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe "Slider Row" do
before do
row_settings = {
title: "Control",
key: :control,
type: :control,
items: ['First', 'Second']
}
@row = Formotion::Row.new(row_settings)
@row.reuse_identifier = 'test'
end

it "should initialize with correct settings" do
@row.object.class.should == Formotion::RowType::ControlRow
end

it "should build cell with segmented control" do
cell = @row.make_cell
cell.accessoryView.class.should == UISegmentedControl
end

# Value
it "should select default value" do
cell = @row.make_cell

cell.accessoryView.selectedSegmentIndex.should == -1
@row.value.should == nil
end

it "should select custom value" do
@row.value = 'Second'
cell = @row.make_cell

cell.accessoryView.selectedSegmentIndex.should == 1
@row.value.should == 'Second'
end
end

0 comments on commit a68068a

Please sign in to comment.