I'm attempting to recreate something like in the iPhone's native weather app, where you can select multiple locations and they are appended as TableViews to a UIScrollView with UIPageControl
I'm starting simple with a hard-coded model in a 2D array. I want each array to go in each tableView, five tableViews total with one row in the first tableView, two in the second, etc:
var model: [[String]] = [
["TableView 1: row 1"],
["TableView 2: row 1", "TableView 2: row 2"],
["TableView 3: row 1", "TableView 3: row 2", "TableView 3: row 3"],
["TableView 4: row 1", "TableView 4: row 2", "TableView 4: row 3", "TableView 4: row 4"],
["TableView 5: row 1", "TableView 5: row 2", "TableView 5: row 3", "TableView 5: row 4", "TableView 5: row 5"]
]
Right now I can only wrap my head around something very simple, like what is shown below, so that the first tableView has 5 cells which say "TableView 1", then the second has five cells which say "TableView 2" and so on:
//number of rows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
//cell for row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
for i in 0...model.count-1{
//grab the correct tableView from the array of tableViews
if tableView == self.tableViews[i]{
//dequeue the cell using a nib
let cell = tableView.dequeueReusableCell(withIdentifier: ProtoTypeTableViewCell.identifier, for: indexPath) as! ProtoTypeTableViewCell
//set text for the label in the nib file
cell.label.text = "TableView \(i+1)"
return cell
}
}
return UITableViewCell()
}
Any help would be greatly appreciated; thank you.
This was quite an interesting question to me since I do like to recreate UIs of popular apps. I think there are a few ways you can achieve this, I will show you one way.
I think the main challenges to solve:
For the UI components, I have chosen to go this way:
Here is how I went about doing this and added comments to explain
Initial set up
Configure your scroll view layout
Configure your stack view layout
Configure your page control layout
Configure and add table views to the stack view
At this stage, if you run this, you were to run the app, you will see the set up is like the weather app without the data in it and the paging control is not wired yet but we have the same number of pages as models (5):
Now what's left is to wire up the page control and the tableview to the model
Update the page view with the scrollview delegate
Implement the table view data source to map the table to the model
Now everything is set up and you get something similar to the weather app with the right data showing in each table view and the page control wired up as well:
Final comments
You can skip the layout parts if you create your UI using frames or in storyboard. I tried to give you something from start to finish that you would run in isolation and you could play around with it by tweaking things.