my problem is quite simple.. All I want is to set up a UITableView
programmatically without the Storyboard.
I created my TableView
and that works fine.. The problem is the UITableViewCell
. It should only contain a UILabel
but I don't get how to initialize it inside the class. And all the tutorials I have watched about UITableViews
are done within Storyboard
.. At least the set-up.
I am new to this and I know this is probably super easy but I couldn't find a solution for my problem. I am grateful for every help!
import UIKit
class WhishCell: UITableViewCell {
var whishText: String
init(whishText: String){
self.whishText = whishText
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
UPDATE
This is how i set up my UITableView
inside my UIViewController
:
let theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.layer.borderWidth = 2.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
and then I also activate
it like the rest of the items inside my UIViewController
:
// constrain tableView
theTableView.view.topAnchor.constraint(equalTo: wishlistView.topAnchor, constant: 180.0),
theTableView.view.bottomAnchor.constraint(equalTo: wishlistView.bottomAnchor, constant: 0),
theTableView.view.leadingAnchor.constraint(equalTo: wishlistView.safeAreaLayoutGuide.leadingAnchor, constant: 30.0),
theTableView.view.trailingAnchor.constraint(equalTo: wishlistView.safeAreaLayoutGuide.trailingAnchor, constant: -30.0),
The TableView
is inside a UIView
which I let appear if the user taps on a button
:
cell.wishlistTapCallback = {
// let wishlistView appear
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
self.wishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
})}