3

I have a UITableView with multiple sections, each having multiple cells with discrete UIStepper controls. Upon tapping and holding each cell’s UIStepper a companion UILabel is updated with the current UIStepper value, as required.

This is working fine: hold the UIStepper down and it increases the total, updating the UILabel as it does so.

What I want is for the header of each section to sum the current total from all the cells in that section, and add that number to the header text, giving the user a quick sum of all the section’s cells UIStepper values.

====================
 Section Total: 12
====================
 Cell: 3   | - | + |
--------------------
 Cell: 5   | - | + |
--------------------
 Cell: 4   | - | + |
====================
 Section Total: 9
====================
 Cell: 1   | - | + |
--------------------
 Cell: 2   | - | + |
--------------------
 Cell: 6   | - | + |

That’s working too, using a [self.tableView reloadData] ; call.

However, it only registers each discrete touch made on the UIStepper; the reloadData call appears to stop the continuous touches being registered. This means to keep the total in the header in sync with the increasing values in that section’s cells requires the user to keep touching the control one tap at a time, rather than a press-and-hold.

Is there any way of reloading a single section’s header text throughout the ‘loop’, so it keeps track of the increasing value as the user holds their finger on the control?

creednmd
  • 2,001
  • 2
  • 18
  • 25

1 Answers1

0

I would keep a reference to all of the section headers you have and in your incrementing code call methods directly on the header view instead of relying on the table update to take care of it. There are two main options to create the section headers:

  1. Initialize all of them in viewDidLoad and keep them in a collection. Then in tableView:viewForHeaderInSection: return the appropriate instance for that section from the collection. This option is best if you know how many sections you will have ahead of time.

  2. Lazily initialize them in tableView:viewForHeaderInSection: and store them in a collection by section number. If you have many headers and you don't want to keep them in memory if the user hasn't scrolled, then this is your best bet.

When you need to update a header, pull it out of the collection and call some incrementing method on it; if it is visible it will update immediately and if not it will appear with the correct value when scrolled into view. Ideally these incremented values are persisted to a model object somewhere!

jszumski
  • 7,430
  • 11
  • 40
  • 53