Class: RMXTableHandler

Inherits:
Object
  • Object
show all
Includes:
RMXCommonMethods
Defined in:
lib/motion/RMXTableHandler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RMXCommonMethods

#dealloc, #description, #inspect

Constructor Details

#initializeRMXTableHandler

Returns a new instance of RMXTableHandler.



26
27
28
29
30
31
# File 'lib/motion/RMXTableHandler.rb', line 26

def initialize
  @sections = []
  @heights = {}
  @registered_reuse_identifiers = {}
  self
end

Instance Attribute Details

#registered_reuse_identifiersObject

Returns the value of attribute registered_reuse_identifiers.



7
8
9
# File 'lib/motion/RMXTableHandler.rb', line 7

def registered_reuse_identifiers
  @registered_reuse_identifiers
end

#sectionsObject

Returns the value of attribute sections.



7
8
9
# File 'lib/motion/RMXTableHandler.rb', line 7

def sections
  @sections
end

Class Method Details

.forTable(tableView) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/motion/RMXTableHandler.rb', line 9

def self.forTable(tableView)
  x = new
  x.tableView = tableView
  tableView.dataSource = x
  tableView.delegate = x
  x
end

Instance Method Details

#animateUpdatesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/motion/RMXTableHandler.rb', line 33

def animateUpdates
  if @isAnimatingUpdates
    @animateUpdatesAgain = true
    return
  end
  @isAnimatingUpdates = true
  CATransaction.begin
  CATransaction.setCompletionBlock(lambda do
    p "animation has finished"
    @isAnimatingUpdates = false
    if @animateUpdatesAgain
      p "animate again!"
      @animateUpdatesAgain = false
      animateUpdates
    end
  end)
  tableView.beginUpdates
  tableView.endUpdates
  CATransaction.commit
end

#numberOfSectionsInTableView(tableView) ⇒ Object



178
179
180
181
182
183
# File 'lib/motion/RMXTableHandler.rb', line 178

def numberOfSectionsInTableView(tableView)
  RMX.assert_main_thread!
  res = @sections.size
  # p "numberOfSectionsInTableView", res
  res
end

#registerClass(klass, forCellReuseIdentifier: reuseIdentifier) ⇒ Object



68
69
70
71
72
# File 'lib/motion/RMXTableHandler.rb', line 68

def registerClass(klass, forCellReuseIdentifier:reuseIdentifier)
  reuseIdentifier = reuseIdentifier.to_s
  registered_reuse_identifiers[reuseIdentifier] = klass.new
  tableView.registerClass(klass, forCellReuseIdentifier:reuseIdentifier)
end

#reloadDataObject



54
55
56
57
58
# File 'lib/motion/RMXTableHandler.rb', line 54

def reloadData
  if tv = tableView
    tv.reloadData
  end
end

#rmx_deallocObject



17
18
19
20
21
22
23
24
# File 'lib/motion/RMXTableHandler.rb', line 17

def rmx_dealloc
  if tv = tableView
    tv.dataSource = nil
    tv.delegate = nil
  end
  RMX.new(self).nil_instance_variables!
  super
end

#set_size_for_data(data, reuseIdentifier: reuseIdentifier) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/motion/RMXTableHandler.rb', line 60

def set_size_for_data(data, reuseIdentifier:reuseIdentifier)
  reuseIdentifier = reuseIdentifier.to_s
  sizerCell = registered_reuse_identifiers[reuseIdentifier]
  sizerCell.data = data
  height = sizerCell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
  updateHeight(height, data:data, reuseIdentifier:reuseIdentifier)
end

#tableView(tableView, didSelectRowAtIndexPath: indexPath) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/motion/RMXTableHandler.rb', line 74

def tableView(tableView, cellForRowAtIndexPath:indexPath)
  RMX.assert_main_thread!
  context = {
    :tableHandler => self,
    :tableView => tableView,
    :indexPath => indexPath,
    :data => delegate.tableHandler(self, dataForSectionName:@sections[indexPath.section])[indexPath.row]
  }
  res = delegate.tableHandler(self, cellOptsForContext:context)
  if res.is_a?(Hash)
    context.update(res)
    unless res[:reuseIdentifier]
      raise ":reuseIdentifier is required"
    end
    reuseIdentifier = res[:reuseIdentifier].to_s
    registered_reuse_identifiers[reuseIdentifier] || registerClass(RMXTableViewCell, forCellReuseIdentifier:reuseIdentifier)
    res = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath:indexPath)
    res.context = context
  end
  # p "cellForRowAtIndexPath", res, context
  res
end

#updateHeight(height, data: data, reuseIdentifier: reuseIdentifier) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/motion/RMXTableHandler.rb', line 200

def updateHeight(height, data:data, reuseIdentifier:reuseIdentifier)
  reuseIdentifier = reuseIdentifier.to_s
  @heights[reuseIdentifier] ||= {}
  heights = @heights[reuseIdentifier]
  current_height = heights[data]
  if current_height != height
    heights[data] = height
    return true
  end
  false
end