I've recently done a few tutorials on working with tableviews and I have most of the basics down. What Im struggling with is working with tableview sections. Each tutorial showed ways to add specific items to a table view section through a struct but what if you want to add an item to a section based off of the sections title? For example, lets say a user creates an account and is listed on a table view by the users location (Virginia). Another user creates an account and their location is San Francisco so they're listed under that. A third user creates an account and is also in Virgina so they're listed with the second user. How can I go about achieving this? Any references or tutorials would help. thanks in advance
You need to sort out your data before "rendering" the table view. This can be done in many ways, f.ex. by storing the data in different arrays. Each time you receive new data, you can call .reloadData()
on your tableview.
This function (from the UITableViewDataSource protocol) will let you decide what each cell should contain. The IndexPath includes both row and section number.
func tableView(UITableView, cellForRowAt: IndexPath)
You should check out the tableview docs: https://developer.apple.com/reference/uikit/uitableview
You need to implement the UITableViewDataSource protocol https://developer.apple.com/reference/uikit/uitableviewdatasource
Make sure to also override these functions to get your tableview showing different sections:
func numberOfSections(in: UITableView)
func tableView(UITableView, numberOfRowsInSection: Int)
func tableView(UITableView, titleForHeaderInSection: Int)