Class: Formotion::RowCellBuilder

Inherits:
Object
  • Object
show all
Extended by:
BW::KVO
Defined in:
lib/formotion/row/row_cell_builder.rb

Class Method Summary collapse

Class Method Details

.make_cell(row) ⇒ Object

PARAMS row.is_a? Formotion::Row RETURNS [cell configured to that row, a UITextField for that row if applicable or nil]



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/formotion/row/row_cell_builder.rb', line 14

def self.make_cell(row)
  cell, text_field = nil

  cell = UITableViewCell.alloc.initWithStyle(row.object.cell_style, reuseIdentifier:row.reuse_identifier)

  cell.accessoryType = cell.editingAccessoryType = UITableViewCellAccessoryNone

  cell.textLabel.text = row.title
  observe(row, "title") do |old_value, new_value|
    cell.textLabel.text = new_value
  end

  if row.image
    Formotion::RowCellBuilder.set_image(cell, row)
    observe(row, "image") do |old_value, new_value|
      Formotion::RowCellBuilder.set_image(cell, row)
    end
  end

  cell.detailTextLabel.text = row.subtitle
  observe(row, "subtitle") do |old_value, new_value|
    cell.detailTextLabel.text = new_value
  end

  edit_field = row.object.build_cell(cell)

  if edit_field and edit_field.respond_to?("accessibilityLabel=")
    label = row.accessibility
    label = row.title unless label
    edit_field.accessibilityLabel = label if label
  end

  [cell, edit_field]
end

.set_image(cell, row) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/formotion/row/row_cell_builder.rb', line 49

def self.set_image(cell, row)
  if row.image.is_a?(NSURL) || (row.image.is_a?(String) && row.image.include?("http"))
    # Use a remote image helper to set the image.
    image_url = row.image
    image_url = NSURL.URLWithString(image_url) unless image_url.is_a?(NSURL)

    placeholder = row.image_placeholder
    placeholder = UIImage.imageNamed(placeholder) if placeholder.is_a?(String)

    if cell.imageView.respond_to?("setImageWithURLRequest:placeholderImage:success:failure:")
      # Use AFNetworking
      request = NSURLRequest.requestWithURL(image_url)
      cell.imageView.setImageWithURLRequest(request, placeholderImage: placeholder, success: ->(request, response, image) {
        cell.imageView.image = image
        cell.setNeedsLayout
      }, failure: ->(request, response, error) {

      })
    elsif cell.imageView.respond_to?("setImageWithURL:placeholderImage:completed:")
      cell.imageView.setImageWithURL(image_url, placeholderImage: placeholder, completed: ->(image, error, cacheType) {
        cell.imageView.image = image
        cell.setNeedsLayout
      })
    elsif cell.imageView.respond_to?("setImageWithURL:placeholder:")
      # Use JMImageCache
      JMImageCache.sharedCache.imageForURL(image_url, completionBlock: ->(downloadedImage) {
          cell.imageView.image = downloadedImage
          cell.setNeedsLayout
      })
    else
      raise "Please add the AFNetworking, SDWebImage, or JMImageCache pods to your project to use remote images in Formotion"
    end

  else
    # Just set the image like normal
    cell.imageView.image = (row.image.is_a? String) ? UIImage.imageNamed(row.image) : row.image
  end
end