diff --git a/lib/ProMotion/table/cell/table_view_cell_module.rb b/lib/ProMotion/table/cell/table_view_cell_module.rb index 02a2ada..f874b0d 100644 --- a/lib/ProMotion/table/cell/table_view_cell_module.rb +++ b/lib/ProMotion/table/cell/table_view_cell_module.rb @@ -2,9 +2,10 @@ module ProMotion module TableViewCellModule include ViewHelper - attr_accessor :data_cell + attr_accessor :data_cell, :table_screen - def setup(data_cell) + def setup(data_cell, screen) + self.table_screen = WeakRef.new(screen) self.data_cell = data_cell # TODO: Some of these need to go away. Unnecessary overhead. @@ -34,15 +35,18 @@ module ProMotion end def set_accessory_view + PM.logger.info data_cell.to_s if data_cell[:accessory][:view] == :switch switch_view = UISwitch.alloc.initWithFrame(CGRectZero) - switch_view.setAccessibilityLabel(data_cell[:accessory][:accessibility_label]) if data_cell[:accessory][:accessibility_label] - switch_view.addTarget(self.closest_parent(UITableView), action: "accessory_toggled_switch:", forControlEvents:UIControlEventValueChanged) - switch_view.on = true if data_cell[:accessory][:value] + switch_view.setAccessibilityLabel(data_cell[:accessory][:accessibility_label] || data_cell[:title]) + switch_view.addTarget(self.table_screen, action: "accessory_toggled_switch:", forControlEvents:UIControlEventValueChanged) + switch_view.on = !!data_cell[:accessory][:value] self.accessoryView = switch_view elsif data_cell[:accessory][:view] self.accessoryView = data_cell[:accessory][:view] self.accessoryView.autoresizingMask = UIViewAutoresizingFlexibleWidth + else + self.accessoryView = nil end self diff --git a/lib/ProMotion/table/data/table_data.rb b/lib/ProMotion/table/data/table_data.rb index 11e6b08..1b6e776 100644 --- a/lib/ProMotion/table/data/table_data.rb +++ b/lib/ProMotion/table/data/table_data.rb @@ -27,7 +27,8 @@ module ProMotion end table_section = self.section(params[:section]) - table_section[:cells].at(params[:index].to_i) + c = table_section[:cells].at(params[:index].to_i) + set_data_cell_defaults c end def delete_cell(params={}) @@ -68,22 +69,6 @@ module ProMotion self.filtered = false end - def table_view_cell(params={}) - if params[:index_path] - params[:section] = params[:index_path].section - params[:index] = params[:index_path].row - end - - data_cell = self.cell(section: params[:section], index: params[:index]) - return UITableViewCell.alloc.init unless data_cell # No data? - - data_cell = self.set_data_cell_defaults(data_cell) - - table_cell = self.create_table_cell(data_cell) - - table_cell - end - def set_data_cell_defaults(data_cell) data_cell[:cell_style] ||= UITableViewCellStyleDefault data_cell[:cell_identifier] ||= build_cell_identifier(data_cell) @@ -99,21 +84,6 @@ module ProMotion data_cell end - def create_table_cell(data_cell) - table_cell = table_view.dequeueReusableCellWithIdentifier(data_cell[:cell_identifier]) - - unless table_cell - data_cell[:cell_style] = UITableViewCellStyleSubtitle - table_cell = data_cell[:cell_class].alloc.initWithStyle(data_cell[:cell_style], reuseIdentifier:data_cell[:cell_identifier]) - table_cell.extend PM::TableViewCellModule unless table_cell.is_a?(PM::TableViewCellModule) - table_cell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin - end - - table_cell.setup(data_cell) - - table_cell - end - def build_cell_identifier(data_cell) ident = "Cell" unless data_cell[:accessory].nil? diff --git a/lib/ProMotion/table/table.rb b/lib/ProMotion/table/table.rb index 0297edc..7f1ef9e 100644 --- a/lib/ProMotion/table/table.rb +++ b/lib/ProMotion/table/table.rb @@ -72,11 +72,13 @@ module ProMotion table_cell = closest_parent(UITableViewCell, switch) index_path = closest_parent(UITableView, table_cell).indexPathForCell(table_cell) - data_cell = cell_at_section_and_index(index_path.section, index_path.row) - data_cell[:accessory][:arguments] ||= {} - data_cell[:accessory][:arguments][:value] = switch.isOn if data_cell[:accessory][:arguments].is_a?(Hash) + if index_path + data_cell = cell_at_section_and_index(index_path.section, index_path.row) + data_cell[:accessory][:arguments] ||= {} + 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]) if data_cell[:accessory][:action] + trigger_action(data_cell[:accessory][:action], data_cell[:accessory][:arguments]) if data_cell[:accessory][:action] + end end def delete_row(index_paths, animation = nil) @@ -89,6 +91,35 @@ module ProMotion table_view.deleteRowsAtIndexPaths(index_paths, withRowAnimation:animation) end + def table_view_cell(params={}) + if params[:index_path] + params[:section] = params[:index_path].section + params[:index] = params[:index_path].row + end + + data_cell = @promotion_table_data.cell(section: params[:section], index: params[:index]) + return UITableViewCell.alloc.init unless data_cell # No data? + + table_cell = create_table_cell(data_cell) + + table_cell + end + + def create_table_cell(data_cell) + table_cell = table_view.dequeueReusableCellWithIdentifier(data_cell[:cell_identifier]) + + unless table_cell + data_cell[:cell_style] = UITableViewCellStyleSubtitle + table_cell = data_cell[:cell_class].alloc.initWithStyle(data_cell[:cell_style], reuseIdentifier:data_cell[:cell_identifier]) + table_cell.extend PM::TableViewCellModule unless table_cell.is_a?(PM::TableViewCellModule) + table_cell.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin + end + + table_cell.setup(data_cell, self) + + table_cell + end + ########## Cocoa touch methods ################# def numberOfSectionsInTableView(table_view) return Array(@promotion_table_data.data).length @@ -114,7 +145,7 @@ module ProMotion end def tableView(table_view, cellForRowAtIndexPath:index_path) - @promotion_table_data.table_view_cell(index_path: index_path) + table_view_cell(index_path: index_path) end def tableView(table_view, heightForRowAtIndexPath:index_path)