Module: ProMotion::Table

Includes:
Styling, Indexable, Longpressable, Refreshable, Searchable, Utils
Included in:
GroupedTableScreen, TableScreen
Defined in:
lib/ProMotion/table/table.rb,
lib/ProMotion/table/table_utils.rb,
lib/ProMotion/table/extensions/indexable.rb,
lib/ProMotion/table/extensions/searchable.rb,
lib/ProMotion/table/extensions/refreshable.rb,
lib/ProMotion/table/extensions/longpressable.rb

Defined Under Namespace

Modules: Indexable, Longpressable, Refreshable, Searchable, TableClassMethods, Utils

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#array_all_members_of?, #index_path_to_section_index

Methods included from Longpressable

#make_longpressable, #on_long_press

Methods included from Indexable

#table_data_index

Methods included from Refreshable

#end_refreshing, #make_refreshable, #refreshView, #start_refreshing

Methods included from Searchable

#create_search_bar, #make_searchable, #searchDisplayController, #searchDisplayControllerWillBeginSearch, #searchDisplayControllerWillEndSearch, #set_searchable_param_defaults

Methods included from Styling

#add, #add_to, #camelize, #closest_parent, #content_height, #content_max, #content_width, #hex_color, #remove, #rgb_color, #rgba_color, #set_attribute, #set_attributes, #view_or_self

Instance Attribute Details

#promotion_table_dataObject (readonly)

Returns the value of attribute promotion_table_data.



10
11
12
# File 'lib/ProMotion/table/table.rb', line 10

def promotion_table_data
  @promotion_table_data
end

Instance Method Details

#accessory_toggled_switch(switch) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/ProMotion/table/table.rb', line 127

def accessory_toggled_switch(switch)
  table_cell = closest_parent(UITableViewCell, switch)
  index_path = closest_parent(UITableView, table_cell).indexPathForCell(table_cell)

  if index_path
    data_cell = promotion_table_data.cell(section: index_path.section, index: index_path.row)
    data_cell[:accessory][:arguments][:value] = switch.isOn if data_cell[:accessory][:arguments].is_a?(Hash)
    trigger_action(data_cell[:accessory][:action], data_cell[:accessory][:arguments], index_path) if data_cell[:accessory][:action]
  end
end

#check_table_dataObject



25
26
27
# File 'lib/ProMotion/table/table.rb', line 25

def check_table_data
  PM.logger.error "Missing #table_data method in TableScreen #{self.class.to_s}." unless self.respond_to?(:table_data)
end

#create_table_cell(data_cell) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ProMotion/table/table.rb', line 151

def create_table_cell(data_cell)
  new_cell = nil
  table_cell = table_view.dequeueReusableCellWithIdentifier(data_cell[:cell_identifier]) || begin
    new_cell = data_cell[:cell_class].alloc.initWithStyle(data_cell[:cell_style], reuseIdentifier:data_cell[:cell_identifier])
    new_cell.extend(PM::TableViewCellModule) unless new_cell.is_a?(PM::TableViewCellModule)
    new_cell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin
    new_cell.clipsToBounds = true # fix for changed default in 7.1
    new_cell.send(:on_load) if new_cell.respond_to?(:on_load)
    new_cell
  end
  table_cell.setup(data_cell, self) if table_cell.respond_to?(:setup)
  table_cell.send(:on_reuse) if !new_cell && table_cell.respond_to?(:on_reuse)
  table_cell
end

#delete_row(index_paths, animation = nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ProMotion/table/table.rb', line 138

def delete_row(index_paths, animation = nil)
  deletable_index_paths = []
  Array(index_paths).each do |index_path|
    delete_cell = false
    delete_cell = send(:on_cell_deleted, self.promotion_table_data.cell(index_path: index_path)) if self.respond_to?("on_cell_deleted:")
    unless delete_cell == false
      self.promotion_table_data.delete_cell(index_path: index_path)
      deletable_index_paths << index_path
    end
  end
  table_view.deleteRowsAtIndexPaths(deletable_index_paths, withRowAnimation: map_row_animation_symbol(animation)) if deletable_index_paths.length > 0
end

#deleteRowsAtIndexPaths(index_paths, withRowAnimation: animation) ⇒ Object



292
293
294
295
# File 'lib/ProMotion/table/table.rb', line 292

def deleteRowsAtIndexPaths(index_paths, withRowAnimation: animation)
  PM.logger.warn "ProMotion expects you to use 'delete_cell(index_paths, animation)'' instead of 'deleteRowsAtIndexPaths(index_paths, withRowAnimation:animation)'."
  delete_row(index_paths, animation)
end

#edit_mode(args = {}) ⇒ Object



177
178
179
180
181
182
# File 'lib/ProMotion/table/table.rb', line 177

def edit_mode(args = {})
  args[:enabled] = false if args[:enabled].nil?
  args[:animated] = true if args[:animated].nil?

  setEditing(args[:enabled], animated:args[:animated])
end

#edit_mode?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/ProMotion/table/table.rb', line 184

def edit_mode?
  !!isEditing
end

#numberOfSectionsInTableView(_) ⇒ Object

Cocoa touch methods #################



189
190
191
# File 'lib/ProMotion/table/table.rb', line 189

def numberOfSectionsInTableView(_)
  self.promotion_table_data.sections.length
end

#original_search_stringObject



96
97
98
# File 'lib/ProMotion/table/table.rb', line 96

def original_search_string
  self.promotion_table_data.original_search_string
end

#screen_setupObject



16
17
18
19
20
21
22
23
# File 'lib/ProMotion/table/table.rb', line 16

def screen_setup
  check_table_data
  set_up_header_footer_views
  set_up_searchable
  set_up_refreshable
  set_up_longpressable
  set_up_row_height
end

#search_stringObject



100
101
102
# File 'lib/ProMotion/table/table.rb', line 100

def search_string
  self.promotion_table_data.search_string
end

#searching?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/ProMotion/table/table.rb', line 92

def searching?
  self.promotion_table_data.filtered
end

#sectionIndexTitlesForTableView(_) ⇒ Object

Set table_data_index if you want the right hand index column (jumplist)



204
205
206
207
208
# File 'lib/ProMotion/table/table.rb', line 204

def sectionIndexTitlesForTableView(_)
  return if self.promotion_table_data.filtered
  return self.table_data_index if self.respond_to?(:table_data_index)
  nil
end


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ProMotion/table/table.rb', line 33

def set_up_header_footer_views
  [:header, :footer].each do |hf_view|
    if self.respond_to?("table_#{hf_view}_view".to_sym)
      view = self.send("table_#{hf_view}_view")
      if view.is_a? UIView
        self.tableView.send(camelize("set_table_#{hf_view}_view:"), view)
      else
        PM.logger.warn "Table #{hf_view} view must be a UIView."
      end
    end
  end
end

#set_up_longpressableObject



79
80
81
82
83
# File 'lib/ProMotion/table/table.rb', line 79

def set_up_longpressable
  if self.class.respond_to?(:get_longpressable) && self.class.get_longpressable
    self.make_longpressable(self.class.get_longpressable_params)
  end
end

#set_up_refreshableObject



69
70
71
72
73
74
75
76
77
# File 'lib/ProMotion/table/table.rb', line 69

def set_up_refreshable
  if self.class.respond_to?(:get_refreshable) && self.class.get_refreshable
    if defined?(UIRefreshControl)
      self.make_refreshable(self.class.get_refreshable_params)
    else
      PM.logger.warn "To use the refresh control on < iOS 6, you need to include the CocoaPod 'CKRefreshControl'."
    end
  end
end

#set_up_row_heightObject



85
86
87
88
89
90
# File 'lib/ProMotion/table/table.rb', line 85

def set_up_row_height
  if self.class.respond_to?(:get_row_height) && params = self.class.get_row_height
    self.view.rowHeight = params[:height]
    self.view.estimatedRowHeight = params[:estimated]
  end
end

#set_up_searchableObject



46
47
48
49
50
51
52
53
# File 'lib/ProMotion/table/table.rb', line 46

def set_up_searchable
  if self.class.respond_to?(:get_searchable) && self.class.get_searchable
    self.make_searchable(content_controller: self, search_bar: self.class.get_searchable_params)
    if self.class.get_searchable_params[:hide_initially]
      self.tableView.contentOffset = CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height)
    end
  end
end

#setup_search_methodObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ProMotion/table/table.rb', line 55

def setup_search_method
  params = self.class.get_searchable_params
  if params.nil?
    return nil
  else
    @search_method || begin
      params = self.class.get_searchable_params
      @search_action = params[:with] || params[:find_by] || params[:search_by] || params[:filter_by]
      @search_action = method(@search_action) if @search_action.is_a?(Symbol) || @search_action.is_a?(String)
      @search_action
    end
  end
end

#table_viewObject



12
13
14
# File 'lib/ProMotion/table/table.rb', line 12

def table_view
  self.view
end

#tableView(_, willDisplayHeaderView: view, forSection: section) ⇒ Object

Section view methods



194
195
196
# File 'lib/ProMotion/table/table.rb', line 194

def tableView(_, numberOfRowsInSection: section)
  self.promotion_table_data.section_length(section)
end

#toggle_edit_mode(animated = true) ⇒ Object



173
174
175
# File 'lib/ProMotion/table/table.rb', line 173

def toggle_edit_mode(animated = true)
  edit_mode({enabled: !editing?, animated: animated})
end

#trigger_action(action, arguments, index_path) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/ProMotion/table/table.rb', line 118

def trigger_action(action, arguments, index_path)
  return PM.logger.info "Action not implemented: #{action.to_s}" unless self.respond_to?(action)
  case self.method(action).arity
  when 0 then self.send(action) # Just call the method
  when 2 then self.send(action, arguments, index_path) # Send arguments and index path
  else self.send(action, arguments) # Send arguments
  end
end

#update_table_data(args = {}) ⇒ Object



166
167
168
169
170
171
# File 'lib/ProMotion/table/table.rb', line 166

def update_table_data(args = {})
  args = { index_paths: args } unless args.is_a?(Hash)

  self.update_table_view_data(self.table_data, args)
  self.promotion_table_data.search(search_string) if searching?
end

#update_table_view_data(data, args = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ProMotion/table/table.rb', line 104

def update_table_view_data(data, args = {})
  self.promotion_table_data.data = data
  if args[:index_paths]
    args[:animation] ||= UITableViewRowAnimationNone

    table_view.beginUpdates
    table_view.reloadRowsAtIndexPaths(Array(args[:index_paths]), withRowAnimation: args[:animation])
    table_view.endUpdates
  else
    table_view.reloadData
  end
  @table_search_display_controller.searchResultsTableView.reloadData if searching?
end