I want to display a UITableView
inside a UIViewController
. This View Controller contains a UISegmentedControl
with two screens (FirstViewController
and SecondViewController
).
The first View Controller is the one that contains the UIViewTable
(please don't mind the second).
When I execute the app in the simulator, everything works fine, but when I try to scroll the table view in the first ViewController, the cells disappear. The only way to make them reappear is to kill the app and reopen it again.
I'm new to iOS development (I come from Android), and I'm obviously missing something here.
I already tried adding a UIViewTable
outside a container UIView
and it works fine. So I'm guessing the problem has to do with the container or the segmented control...
Here's my implementation:
- Storyboard
UIViewController
with UISegmentedControl
and UIView
(which will contain the two screens of the segmented control).
View Controller
@IBOutlet weak var container: UIView! var sectionViews:[UIView]! override func viewDidLoad() { super.viewDidLoad() sectionViews = [UIView]() sectionViews.append(FirstViewController().view) sectionViews.append(SecondViewController().view) for v in sectionViews { container.addSubview(v) } container.bringSubviewToFront(sectionViews[0]) } @IBAction func switchViewsAction(_ sender: UISegmentedControl) { self.container.bringSubviewToFront(self.sectionViews[sender.selectedSegmentIndex]) }
First View Controller
The FirstViewController
has a swift
and a xib
files, and has two files Cell.swift
and Cell.xib
for the table cell.
FirstViewController.swift
@IBOutlet weak var tableView: UITableView!
let cellID = "CellId"
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: cellID)
self.tableView.dataSource = self
self.tableView.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! Cell
cell.label.text = "\(indexPath.row)"
return cell
}
FirstViewController.xib
Cell.xib
Any help will be appreciated! Thanks