Module: ProMotion::TableViewCellModule

Includes:
Styling
Included in:
TableViewCell
Defined in:
lib/ProMotion/table/cell/table_view_cell_module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Styling

#add, #add_to, #closest_parent, #content_height, #hex_color, #remove, #rgb_color, #rgba_color, #set_attribute, #set_attributes, #set_easy_attributes, #view_or_self

Methods included from Conversions

#camel_case, #convert_symbol, #objective_c_method_name

Instance Attribute Details

#data_cellObject

Returns the value of attribute data_cell.



5
6
7
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 5

def data_cell
  @data_cell
end

#table_screenObject

Returns the value of attribute table_screen.



5
6
7
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 5

def table_screen
  @table_screen
end

Instance Method Details

#set_accessory_viewObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 32

def set_accessory_view
  if data_cell[:accessory]
    if data_cell[:accessory][:view] == :switch
      switch_view = UISwitch.alloc.initWithFrame(CGRectZero)
      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
    end
  else
    self.accessoryView = nil
  end

  self
end

#set_cell_attributesObject



25
26
27
28
29
30
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 25

def set_cell_attributes
  data_cell_attributes = data_cell.dup
  [:image, :accessory_action, :editing_style].each { |k| data_cell_attributes.delete(k) }
  set_attributes self, data_cell_attributes
  self
end

#set_detailsObject



109
110
111
112
113
114
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 109

def set_details
  if data_cell[:details]
    self.addSubview data_cell[:details][:image]
  end
  self
end

#set_imageObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 79

def set_image
  if data_cell[:image]

    cell_image = data_cell[:image].is_a?(Hash) ? data_cell[:image][:image] : data_cell[:image]
    cell_image = UIImage.imageNamed(cell_image) if cell_image.is_a?(String)

    self.imageView.layer.masksToBounds = true
    self.imageView.image = cell_image
    self.imageView.layer.cornerRadius = data_cell[:image][:radius] if data_cell[:image].is_a?(Hash) && data_cell[:image][:radius]
  end
  self
end

#set_remote_imageObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 60

def set_remote_image
  if data_cell[:remote_image]
    if self.imageView.respond_to?("setImageWithURL:placeholderImage:")
      url = data_cell[:remote_image][:url]
      url = NSURL.URLWithString(url) unless url.is_a?(NSURL)
      placeholder = data_cell[:remote_image][:placeholder]
      placeholder = UIImage.imageNamed(placeholder) if placeholder.is_a?(String)

      self.image_size = data_cell[:remote_image][:size] if data_cell[:remote_image][:size] && self.respond_to?("image_size=")
      self.imageView.setImageWithURL(url, placeholderImage: placeholder)
      self.imageView.layer.masksToBounds = true
      self.imageView.layer.cornerRadius = data_cell[:remote_image][:radius] if data_cell[:remote_image].has_key?(:radius)
    else
      PM.logger.error "ProMotion Warning: to use remote_image with TableScreen you need to include the CocoaPod 'SDWebImage'."
    end
  end
  self
end

#set_selection_styleObject



144
145
146
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 144

def set_selection_style
  self.selectionStyle = UITableViewCellSelectionStyleNone if data_cell[:no_select]
end

#set_stylesObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 116

def set_styles
  if data_cell[:styles] && data_cell[:styles][:label] && data_cell[:styles][:label][:frame]
    ui_label = false
    self.contentView.subviews.each do |view|
      if view.is_a? UILabel
        ui_label = true
        view.text = data_cell[:styles][:label][:text]
      end
    end

    unless ui_label == true
      label ||= UILabel.alloc.initWithFrame(CGRectZero)
      set_attributes label, data_cell[:styles][:label]
      self.contentView.addSubview label
    end

    # TODO: What is this and why is it necessary?
    self.textLabel.textColor = UIColor.clearColor
  else
    cell_title = data_cell[:title]
    cell_title ||= ""
    self.textLabel.backgroundColor = UIColor.clearColor
    self.textLabel.text = cell_title
  end

  self
end

#set_subtitleObject



51
52
53
54
55
56
57
58
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 51

def set_subtitle
  if data_cell[:subtitle] && self.detailTextLabel
    self.detailTextLabel.text = data_cell[:subtitle]
    self.detailTextLabel.backgroundColor = UIColor.clearColor
    self.detailTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth
  end
  self
end

#set_subviewsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 92

def set_subviews
  tag_number = 0
  Array(data_cell[:subviews]).each do |view|
    # Remove an existing view at that tag number
    tag_number += 1
    existing_view = self.viewWithTag(tag_number)
    existing_view.removeFromSuperview if existing_view

    # Add the subview if it exists
    if view
      view.tag = tag_number
      self.addSubview view
    end
  end
  self
end

#setup(data_cell, screen) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ProMotion/table/cell/table_view_cell_module.rb', line 7

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.
  set_cell_attributes
  set_accessory_view
  set_subtitle
  set_image
  set_remote_image
  set_subviews
  set_details
  set_styles
  set_selection_style

  self
end