1
votes

I'm currently developing an app in Xcode. Within this app I want to give the user the possibility to filter the content of Table View Cells (objects) by multiple checkboxes [see picture 1].

Picture 1

Every object thats shown in the Table View has 8 properties which have the value 1, 2 or 3. The checkboxes shown in the filter area are in relationship with these 8 properties.

Like:

  • Checkbox1 = prop1
  • Checkbox2 = prop2
  • Checkbox3 = prop3
  • etc..

Objects arrays:

var objects:[Object] = [
Object(name: "Lorem Ipsum", prop1: "3", prop2: "1", prop3: "1", prop4: "1", prop5: "1", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "1", prop2: "1", prop3: "1", prop4: "3", prop5: "1", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "1", prop2: "2", prop3: "2", prop4: "2", prop5: "2", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "2", prop2: "3", prop3: "3", prop4: "1", prop5: "3", prop6: "1", prop7: "3", prop8: "2"),
Object(name: "Lorem Ipsum", prop1: "1", prop2: "3", prop3: "1", prop4: "2", prop5: "2", prop6: "3", prop7: "2", prop8: "1"),
Object(name: "Lorem Ipsum", prop1: "2", prop2: "3", prop3: "1", prop4: "1", prop5: "1", prop6: "1", prop7: "3", prop8: "2")
]

When all checkboxes are unchecked all the objects will show the same default color (blue).

But when one of the checkboxes is checked, the filter needs to change the color of the objects that have the related property to the selected checkbox, as value 2 or 3. When the value is 2 = change to green color, and when the value is 3 = change to purple color.

So for instance; if checkbox2 is selected, all objects with prop2 as 2 or 3 should have a different color [see picture 2]

Picture 2 (checkbox2 selected)

I did research but couldn't find a way to do it and make it work. If somebody could help, that would be amazing!

EDIT-NOTE: If the user selects two checkboxes, for instance 1 and 2, the objects with value 3 on property 1 or 2 needs to have the color purple, even if they have a value 2 on one of these properties. So value 3 'weighs' more than 2

1
Can you not simply reload the current cell that is showing the checkboxes? tableView.reloadRows(at: [currentIndexPath], with: .automatic) - Nathaniel
Yes that would be possible, but then I have to render a lot of arrays right? And what if the user selects multiple checkboxes. - Maarten
If I am understanding your question correctly, I don't think you have any choice but to reload the tableView data everytime a checkbox is selected/deselected unless you want to add observers to each cell...which seems excessive.... If you want a smoother animation for the refresh, use tableView.reloadSections(IndexSet(integer: 0), with: .automatic) and select the animation you prefer best. - Nathaniel
Alright, that sounds clear to me. Thanks! My bigger problem is that I can't find a way to make the filters work. From my research i found the solution to just make 6 buttons as the checkboxes, which can operate as the filter for the objects. I'm kinda new to xcode, so i find it hard to write the function to make the buttons check the particular properties of the objects. When this function is executed, the table should reload like you mentioned above. Do you maybe have an idea for me how to build the function behind these buttons? - Maarten

1 Answers

0
votes

I would suggest maybe using a dictionary for all the checkboxes. Assign each checkbox a unique tag value (0-7).

let filters: [Int: Bool] = [0: false, 1: false ... 7:0]

When one is selected via a fund didSelect(checkbox: UIButton){ self.tableView.reloadData() }, you would update the corresponding filter by setting self.filters[checkbox.tag] = true.

Then your tableView data items should be filtered by iterating through all the filters and checking if they are true/false before assigning a color for the cell using cellForRow(at: IndexPath).