[SWIFT] When using FloatingPanel, the tableView created by Stroybard becomes nil

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.

スクリーンショット 2020-12-25 21.58.58.png

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.

スクリーンショット 2020-12-25 22.00.12.png

bonus

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

reference

https://ymgsapo.com/2020/08/27/swift-floating-panel/

Recommended Posts

When using FloatingPanel, the tableView created by Stroybard becomes nil
When yum becomes unusable
When using FloatingPanel, the tableView created by Stroybard becomes nil
LIMIT 11 when data is acquired by irb
A note when the heroku command becomes unavailable
Solution when the image data becomes nil via the confirmation screen
When using the constructor of Java's Date class, the date advances by 1900.
When using JDBC, NLS parameters are affected by the Java locale.
[Using Eclipse] Precautions when moving the workspace being created to another PC
Let's specify the version when using docker