teacup 2.0.6 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- teacup (2.0.6)
4
+ teacup (2.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -141,6 +141,8 @@ constraints. Teacup can also integrate with the [motion-layout][] gem!
141
141
  * [Style via UIView Class](#style-via-uiview-class)
142
142
  * [Importing stylesheets](#importing-stylesheets)
143
143
  * [Style via UIAppearance](#style-via-uiappearance) (iOS only)
144
+ * [UITableViews](#uitableviews) - This is important if you are using styles and
145
+ constraints in a `UITableViewDelegate`.
144
146
  * [More Teacup features](#more-teacup-features)
145
147
  * [Styling View Properties](#styling-view-properties)
146
148
  * [Orientation Styles](#orientation-styles) (iOS only)
@@ -156,7 +158,7 @@ constraints. Teacup can also integrate with the [motion-layout][] gem!
156
158
  * [Showdown](#showdown)
157
159
  * [The Nitty Gritty](#the-nitty-gritty)
158
160
  * [Advanced Teacup Tricks](#advanced-teacup-tricks)
159
- * [UITableViewCell](#uitableviewcell)
161
+ * [Including `Teacup::Layout` on arbitrary classes](#including-teacup-layout-on-arbitrary-classes)
160
162
  * [Sweettea](#sweettea)
161
163
  * [Misc notes](#misc-notes)
162
164
  * [The Dummy](#the-dummy)
@@ -633,6 +635,49 @@ end
633
635
  @label.style(textColor: UIColor.blueColor, text: 'Blue Label')
634
636
  ```
635
637
 
638
+ UITableViews
639
+ ------------
640
+
641
+ Teacup is designed to be used in coordination with the controller life cycle,
642
+ but there are other life cycles that need to be considered as well.
643
+ UITableViews maintain a "queue" of cells that can be reused, and they need to be
644
+ restyled when the cell is created and re-used.
645
+
646
+ The solution is to apply the styles and layout constraints inside the
647
+ `tableView:willDisplayCell:forRowAtIndexPath:` delegate method. In your
648
+ delegate, if you include the `Teacup::TableViewDelegate` module, you'll get this
649
+ behavior for free, and if you override this method, you can call `super` to have
650
+ the Teacup method run.
651
+
652
+ ```ruby
653
+ class TableViewController < UITableViewController
654
+ include Teacup::TableViewDelegate
655
+
656
+ stylesheet :table
657
+
658
+ def tableView(table_view, cellForRowAtIndexPath:index_path)
659
+ cell = table_view.dequeueReusableCellWithIdentifier('cell id')
660
+
661
+ layout(cell.contentView, :root) do
662
+ cell.title_label = subview(UILabel, :title_label, :text => "title #{index_path.row}")
663
+ cell.details_label = subview(UILabel, :details_label, :text => "details #{index_path.row}")
664
+ cell.other_label = subview(UILabel, :other_label, :text => "other #{index_path.row}")
665
+ end
666
+
667
+ return cell
668
+ end
669
+
670
+ # This method is implemented by the Teacup::TableViewDelegate. If you need
671
+ # to implement it, be sure to call super.
672
+ # def tableView(tableView, willDisplayCell:cell, forRowAtIndexPath:indexPath)
673
+ # super
674
+ # end
675
+ end
676
+ ```
677
+
678
+ Constraints and styles get applied before the view appears, even if the cell is
679
+ reused later.
680
+
636
681
  More Teacup features
637
682
  --------------------
638
683
 
@@ -1224,7 +1269,7 @@ More examples!
1224
1269
 
1225
1270
  ```ruby
1226
1271
  class MyController < UIViewController
1227
- stylyesheet :my_sheet
1272
+ stylesheet :my_sheet
1228
1273
  layout do
1229
1274
  subview(UILabel, :label, text: 'overrides')
1230
1275
  end
@@ -1276,17 +1321,19 @@ not in the context of a `UIViewController` that things get hairy.
1276
1321
  If this is the case, you should get some pretty obvious warning messages,
1277
1322
  something along the lines of `Could not find :superview`.
1278
1323
 
1279
- ### UITableViewCell
1280
-
1281
- If you are using your controller as your tableview dataSource this is not a big
1282
- deal, the `subview` and `layout` methods continue to work as you expect them to.
1324
+ ### Including `Teacup::Layout` on arbitrary classes
1283
1325
 
1284
1326
  I don't know about you, but I often write helper classes for tableviews that
1285
1327
  appear on many screens in an app. You should not shy away from adding teacup's
1286
1328
  `Layout` module to these helper classes.
1287
1329
 
1330
+ If you are using your controller as your table view dataSource, the `subview`
1331
+ and `layout` methods continue to work as you expect them to. This is for the
1332
+ case when you are using a helper class.
1333
+
1288
1334
  ```ruby
1289
1335
  class TableHelper
1336
+ include Teacup::TableViewDelegate
1290
1337
  include Teacup::Layout
1291
1338
 
1292
1339
  stylesheet :table_helper
data/app/app_delegate.rb CHANGED
@@ -6,7 +6,9 @@ class AppDelegate
6
6
  application.setStatusBarHidden(true, withAnimation:UIStatusBarAnimationSlide)
7
7
 
8
8
  @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
9
- ctlr = MotionLayoutController.new
9
+ # change this controller to whatever controller you want to test, or are
10
+ # writing tests for.
11
+ ctlr = TableViewController.new
10
12
  @window.rootViewController = ctlr
11
13
  @window.rootViewController.wantsFullScreenLayout = true
12
14
  @window.makeKeyAndVisible
@@ -0,0 +1,106 @@
1
+ class TableViewController < UITableViewController
2
+ include Teacup::TableViewDelegate
3
+
4
+ stylesheet :table
5
+
6
+ def viewDidLoad
7
+ self.view.registerClass(CustomCell, forCellReuseIdentifier:'cell id')
8
+ end
9
+
10
+ def numberOfSectionsInTableView(table_view)
11
+ 1
12
+ end
13
+
14
+ def tableView(table_view, numberOfRowsInSection:section)
15
+ 10
16
+ end
17
+
18
+ def tableView(tableView, heightForRowAtIndexPath:index_path)
19
+ 100
20
+ end
21
+
22
+ def tableView(table_view, cellForRowAtIndexPath:index_path)
23
+ cell = table_view.dequeueReusableCellWithIdentifier('cell id')
24
+
25
+ # for testing cell reuse
26
+ if cell.is_reused.nil?
27
+ cell.is_reused = false
28
+ else
29
+ cell.is_reused ||= true
30
+ end
31
+
32
+ cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
33
+
34
+ cell.backgroundView = layout(UIView, :bg)
35
+
36
+ layout(cell.contentView, :root) do
37
+ subview(UIView, :padding) do
38
+ cell.title_label = subview(UILabel, :cell_title_label, :text => "title #{index_path.row}")
39
+ cell.details_label = subview(UILabel, :cell_details_label, :text => "details #{index_path.row}")
40
+ cell.other_label = subview(UILabel, :cell_other_label, :text => "other #{index_path.row}")
41
+ end
42
+ end
43
+
44
+ return cell
45
+ end
46
+
47
+ def tableView(table_view, didSelectRowAtIndexPath:index_path)
48
+ table_view.deselectRowAtIndexPath(index_path, animated:true)
49
+ end
50
+
51
+ def tableView(tableView, willDisplayCell:cell, forRowAtIndexPath:indexPath)
52
+ super
53
+ # one more change, to prove that this method can be overridden
54
+ cell.title_label.textColor = UIColor.blueColor
55
+ end
56
+
57
+ end
58
+
59
+
60
+ class CustomCell < UITableViewCell
61
+ attr_accessor :title_label, :details_label, :other_label, :is_reused
62
+ end
63
+
64
+
65
+ Teacup::Stylesheet.new :table do
66
+ style :bg,
67
+ backgroundColor: UIColor.colorWithRed(247.0 / 256.0, green:221.0 / 256.0, blue:186.0 / 256.0, alpha:1)
68
+
69
+ style :padding,
70
+ backgroundColor: UIColor.greenColor,
71
+ constraints: [
72
+ constrain(:width).equals(:superview, :width).times(0.96),
73
+ constrain(:height).equals(:superview, :height).times(0.96),
74
+ constrain(:center_x).equals(:superview, :center_x),
75
+ constrain(:center_y).equals(:superview, :center_y)
76
+ ]
77
+
78
+ style :cell_title_label,
79
+ font: UIFont.boldSystemFontOfSize(17),
80
+ constraints: [
81
+ constrain_height(20),
82
+ constrain(:top).equals(:superview, :top),
83
+ constrain(:width).equals(:superview, :width),
84
+ constrain(:center_x).equals(:superview, :center_x)
85
+ ]
86
+
87
+ style :cell_details_label,
88
+ font: UIFont.systemFontOfSize(14),
89
+ color: UIColor.grayColor,
90
+ constraints: [
91
+ constrain_height(17),
92
+ constrain_below(:cell_title_label, 5),
93
+ constrain(:width).equals(:superview, :width),
94
+ constrain(:center_x).equals(:superview, :center_x)
95
+ ]
96
+
97
+ style :cell_other_label,
98
+ font: UIFont.systemFontOfSize(14),
99
+ color: UIColor.grayColor,
100
+ constraints: [
101
+ constrain_height(17),
102
+ constrain_below(:cell_details_label, 5),
103
+ constrain(:width).equals(:superview, :width),
104
+ constrain(:center_x).equals(:superview, :center_x)
105
+ ]
106
+ end
@@ -1,5 +1,5 @@
1
1
  module Teacup
2
2
 
3
- VERSION = '2.0.6'
3
+ VERSION = '2.1.0'
4
4
 
5
5
  end
@@ -0,0 +1,12 @@
1
+ module Teacup
2
+
3
+ module TableViewDelegate
4
+
5
+ def tableView(tableView, willDisplayCell:cell, forRowAtIndexPath:indexPath)
6
+ cell.restyle!
7
+ cell.apply_constraints
8
+ end
9
+
10
+ end
11
+
12
+ end
@@ -0,0 +1,93 @@
1
+ describe "TableViewCells" do
2
+ tests TableViewController
3
+
4
+ describe "Style tests on cell[0]" do
5
+ before do
6
+ path = NSIndexPath.indexPathWithIndex(0).indexPathByAddingIndex(0)
7
+ @cell = controller.view.cellForRowAtIndexPath(path)
8
+ @padding = @cell.contentView.subviews[0]
9
+ end
10
+
11
+ it "should have styled padding" do
12
+ @padding.backgroundColor.should == UIColor.greenColor
13
+ @padding.center.x.should == @padding.superview.bounds.size.width / 2
14
+ @padding.center.y.should == @padding.superview.bounds.size.height / 2
15
+ @padding.frame.size.width.should < @padding.superview.bounds.size.width
16
+ @padding.frame.size.height.should < @padding.superview.bounds.size.height
17
+ end
18
+
19
+ it "should have styled title" do
20
+ @cell.title_label.text.should =~ /^title/
21
+ @cell.title_label.frame.origin.x.should == 0
22
+ @cell.title_label.frame.origin.y.should == 0
23
+ @cell.title_label.frame.size.height.should == 20
24
+ @cell.title_label.frame.size.width.should == @padding.frame.size.width
25
+ @cell.title_label.textColor.should == UIColor.blueColor
26
+ end
27
+
28
+ it "should have styled details" do
29
+ @cell.details_label.text.should =~ /^details/
30
+ @cell.details_label.frame.origin.x.should == 0
31
+ @cell.details_label.frame.origin.y.should == CGRectGetMaxY(@cell.title_label.frame) + 5
32
+ @cell.details_label.frame.size.height.should == 17
33
+ @cell.details_label.frame.size.width.should == @padding.frame.size.width
34
+ end
35
+
36
+ it "should have styled other" do
37
+ @cell.other_label.text.should =~ /^other/
38
+ @cell.other_label.frame.origin.x.should == 0
39
+ @cell.other_label.frame.origin.y.should == CGRectGetMaxY(@cell.details_label.frame) + 5
40
+ @cell.other_label.frame.size.height.should == 17
41
+ @cell.other_label.frame.size.width.should == @padding.frame.size.width
42
+ end
43
+
44
+ end
45
+
46
+ describe "Style tests on cell[9]" do
47
+ before do
48
+ path = NSIndexPath.indexPathWithIndex(0).indexPathByAddingIndex(9)
49
+ controller.view.scrollToRowAtIndexPath(path, atScrollPosition:UITableViewScrollPositionBottom, animated:false)
50
+ @cell = controller.view.cellForRowAtIndexPath(path)
51
+ @padding = @cell.contentView.subviews[0]
52
+ end
53
+
54
+ it "should be a reused cell" do
55
+ @cell.is_reused.should == true
56
+ end
57
+
58
+ it "should have styled padding" do
59
+ @padding.backgroundColor.should == UIColor.greenColor
60
+ @padding.center.x.should == @padding.superview.bounds.size.width / 2
61
+ @padding.center.y.should == @padding.superview.bounds.size.height / 2
62
+ @padding.frame.size.width.should < @padding.superview.bounds.size.width
63
+ @padding.frame.size.height.should < @padding.superview.bounds.size.height
64
+ end
65
+
66
+ it "should have styled title" do
67
+ @cell.title_label.text.should =~ /^title/
68
+ @cell.title_label.frame.origin.x.should == 0
69
+ @cell.title_label.frame.origin.y.should == 0
70
+ @cell.title_label.frame.size.height.should == 20
71
+ @cell.title_label.frame.size.width.should == @padding.frame.size.width
72
+ @cell.title_label.textColor.should == UIColor.blueColor
73
+ end
74
+
75
+ it "should have styled details" do
76
+ @cell.details_label.text.should =~ /^details/
77
+ @cell.details_label.frame.origin.x.should == 0
78
+ @cell.details_label.frame.origin.y.should == CGRectGetMaxY(@cell.title_label.frame) + 5
79
+ @cell.details_label.frame.size.height.should == 17
80
+ @cell.details_label.frame.size.width.should == @padding.frame.size.width
81
+ end
82
+
83
+ it "should have styled other" do
84
+ @cell.other_label.text.should =~ /^other/
85
+ @cell.other_label.frame.origin.x.should == 0
86
+ @cell.other_label.frame.origin.y.should == CGRectGetMaxY(@cell.details_label.frame) + 5
87
+ @cell.other_label.frame.size.height.should == 17
88
+ @cell.other_label.frame.size.width.should == @padding.frame.size.width
89
+ end
90
+
91
+ end
92
+
93
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teacup
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-26 00:00:00.000000000 Z
12
+ date: 2013-06-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'Teacup is a community-driven DSL for making CSS-like styling, and
15
15
  layouts for
@@ -43,6 +43,7 @@ files:
43
43
  - app/controllers/main_controller.rb
44
44
  - app/controllers/motion_layout_controller.rb
45
45
  - app/controllers/present_modal_controller.rb
46
+ - app/controllers/table_view_controller.rb
46
47
  - app/controllers/tableview_controller.rb
47
48
  - app/custom_class.rb
48
49
  - app/styles/appearance.rb
@@ -57,6 +58,7 @@ files:
57
58
  - lib/teacup-ios/style.rb
58
59
  - lib/teacup-ios/stylesheet_extensions/autoresize.rb
59
60
  - lib/teacup-ios/stylesheet_extensions/device.rb
61
+ - lib/teacup-ios/table_view/table_view_delegate.rb
60
62
  - lib/teacup-osx/core_extensions/ns_view.rb
61
63
  - lib/teacup-osx/core_extensions/ns_view_controller.rb
62
64
  - lib/teacup-osx/core_extensions/ns_window.rb
@@ -143,6 +145,7 @@ files:
143
145
  - spec/ios/stylesheet_extensions/autoresize_spec.rb
144
146
  - spec/ios/stylesheet_extensions/device_spec.rb
145
147
  - spec/ios/stylesheet_spec.rb
148
+ - spec/ios/table_view/table_view_spec.rb
146
149
  - spec/ios/ui_view_getters_spec.rb
147
150
  - spec/ios/uiswitch_spec.rb
148
151
  - spec/ios/view_spec.rb
@@ -186,6 +189,7 @@ test_files:
186
189
  - spec/ios/stylesheet_extensions/autoresize_spec.rb
187
190
  - spec/ios/stylesheet_extensions/device_spec.rb
188
191
  - spec/ios/stylesheet_spec.rb
192
+ - spec/ios/table_view/table_view_spec.rb
189
193
  - spec/ios/ui_view_getters_spec.rb
190
194
  - spec/ios/uiswitch_spec.rb
191
195
  - spec/ios/view_spec.rb