1

I want to loop through a TableView and extract the text from all the selected rows. I suppose I "could" create and maintain a special array that is updated every time a row is selected/deselected using the didSelect/didDeselectRowAtIndexPath methods. But creating a separate array seems like an extra step. Is there no way to let the TableView itself serve as the array and then simply loop through it and get the selected rows? What would the code look like? I'm new to Swift, so this might be a silly question.

dimery2006
  • 335
  • 1
  • 5
  • 13
  • 4
    Table views consume data and display it, they are not a data source. You should track selection and then access the data from the underlying data source. See the `indexPathsForSelectedRows` method on `UITableView` – Paulw11 Feb 10 '16 at 00:46
  • Thanks, Paul. It does seem a bit inefficient that tableviews display data, without allowing the access to it. But, I'm sure there's probably a good reason for that. – dimery2006 Feb 10 '16 at 01:12
  • It is because they are a 'view' - In the Model-View-Controller paradigm the data model is separated from the view and from the controller. This means that you could change the view from a table view to, say, a collection view without there being any change to your data model – Paulw11 Feb 10 '16 at 01:13

3 Answers3

0

Part of the problem is that cells are supposed to be reused, and when used this way it is not possible to loop through them all. You could get around this by using a unique reuse identifier for each cell, such as the indexPath itself or some underlying unique id in your model. Then, you could indeed loop through all cells and retrieve whatever state you desired from each.

You would, however, find your application crushed under the weight of too many cells being instantiated and kept in memory. If you don't have many cells you won't be killed, but try it with a big data set and your app will enjoy a very quick death.

It is far more efficient to store one array with a bunch of id's than a large number of memory-intensive UITableViewCells.

Mitch Cohen
  • 1,551
  • 3
  • 15
  • 29
0

As mentioned in comments, you should work with underlying datasource, not the table itself.

For example if your table shows rows from Array, it is way more faster to retrieve strings directly from that array than creating UITableViewCells and get strings from them.

  1. Get indices of selected rows using UITableView's property indexPathsForSelectedRows.
  2. Query datasource for each row.
Evgeny Sureev
  • 1,008
  • 10
  • 16
0

As has been said the tableview only handles displaying, your datasource is what powers the data shown if you think about it.

Plus as said before the tableview dequeues cells as they scroll on and off the screen.

The best way to achieve what you want is to add a property to your datasource for each element that will allow you to filter out the select properties easily.

How are you storing the state for each selected cell currently? As this is the same functionally you would use to be able to generate your selected text array.

will
  • 944
  • 9
  • 18