In conclusion, let's generate a controller to float from the storyboard.
I used Floating Panel to make something like TikTok's comment section with a little practice, but at that time tableView became nil and I was addicted to it.
IBOutlet was properly associated with Chitin, but apparently it was generated from Class when contentView was created with ViewController, so it seems that the tableview placed on the Storyboard was not loaded and became nil.
Therefore, I was able to solve it safely by using the fromStoryboard ()
function to generate a view controller from the storyboard at the time of generation.
ViewController.swift
import UIKit
import FloatingPanel
class ViewController: UIViewController, FloatingPanelControllerDelegate {
var fpc: FloatingPanelController!
override func viewDidLoad() {
super.viewDidLoad()
fpc = FloatingPanelController()
fpc.delegate = self // Optional //Not required when not changing the layout etc.
//let contentVC = CommenttViewController()
let contentVC = CommentViewController.fromStoryboard()
fpc.set(contentViewController: contentVC)
// Track a scroll view(or the siblings) in the content view controller.
fpc.track(scrollView: contentVC.tableView)
// Add and show the views managed by the `FloatingPanelController` object to self.view.
fpc.addPanel(toParent: self)
}
}
CommentViewController.swift
import UIKit
class CommentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var commentCountLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self //Here the tableView became nil and fell
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
static func fromStoryboard(_ storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)) -> CommentViewController {
let controller = storyboard.instantiateViewController(withIdentifier: "CommentViewController") as! CommentViewController
return controller
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CommentCell", for: indexPath) as! CommentCell
return cell
}
}
Note that you need to set the Storyboard ID
to generate a view controller from the storyboard.
In addition, I always forget it, but if you make it by putting ProtoTypeCell directly in tableView, it seems that the following register is not necessary. However, I forgot the difference between registration with nib and registration with Class, so I'd be happy if you could tell me with a kind person's comment.
tableView.register(UINib(nibName: "CommentCell", bundle: nil), forCellReuseIdentifier: "CommentCell") //When you design a Cell by creating a separate xib file instead of a storyboard?
tableView.register(CommentCell.self, forCellReuseIdentifier: "CommentCell") //
Also, if dataSource and delegate are linked in Outlets of Connections Inspector of the storyboard, the following is unnecessary, but I am doing it with code.
tableView.dataSource = self
tableView.delegate = self
https://ymgsapo.com/2020/08/27/swift-floating-panel/
Recommended Posts