Filter table partitions using a segmented control

I am wondering what is the best way to filter out various sections in a table view using a segmented control in a toolbar.

Say, for example, I have a tabular view with cells in three different sections - Friday, Saturday, and Sunday. I also have a toolbar on top of 4 segments: everything, Friday, Saturday and Sunday. When “All” is selected on the segmented element, all sections and cells in the table should be displayed. When Friday is selected in the segmented element, only cells in the Friday section should be displayed in the table. And so on and so forth.

Where in the code for the table view controller can I do this? I imagine this is a pretty simple thing to achieve, so are there any other examples of this that someone can point me to?

Thanks in advance!

+4
source share
1 answer

One place for this in the UITableViewDataSource code: to give your data source access to your UISegmentedControl , allowing it to access the value of its selectedSegmentIndex property. There, your tableView:numberOfRowsInSection: can find the right number of rows by looking at the index and subtracting the number of inapplicable rows, and tableView:cellForRowAtIndexPath: can re-index the rows based on the current selection.

You may need to store additional data structures in the model in order to support the tableView:cellForRowAtIndexPath: method. In particular, you may want to add arrays for three “special” days, so that the search for “which row is number three if selected on Fridays” does not need to iterate over all the data in search of the third applicable row.

+3
source

Source: https://habr.com/ru/post/1435410/


All Articles