Datepicker won't display in custom UITableViewCell class - ios

I try to keep a separate custom cell class, but somehow the datepicker I dragged into it from storyboard refuses to show up after I ran it.
import UIKit
class eventViewController: UIViewController,UITextViewDelegate,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var timeTableView: UITableView!
#IBOutlet weak var eventText: UITextView!
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.eventText.delegate = self
self.scrollView.delegate = self
self.timeTableView.delegate = self
self.timeTableView.dataSource = self
self.timeTableView.register(startDateCell.self, forCellReuseIdentifier: "startDateCell")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "startDateCell", for: indexPath) as! startDateCell
//cell.startDatePicker.date = Date()
//cell.text = "anything"
return cell
}
}
class startDateCell: UITableViewCell {
#IBOutlet weak var startDatePicker: UIDatePicker!
}
I have the outlet connected to my datePicker in the custom cell and I also defined my custom cell class to be startDateCell in inspector, and I registered my custom cell in my view controller. It will throw a runtime error saying the datepicker is nil.
Can somebody explain why it isn't showing up?

Can you remove the following code from viewDidLoad() and ensure you have set reuse identifier of custom tableviewcell as "startDateCell" in storyboard and try again?
self.timeTableView.register(startDateCell.self,
forCellReuseIdentifier: "startDateCell")

Related

Why can I not cast this cell to a class that inherits from cell in swift?

I am creating a TableView in swift using the UITableViewCOntroller, which I have done in several other apps, however this time when I try to cast my cell to the custom cell class I get the error that it cannot be cast.
Here is my code:
class CollapseNoteViewController: UITableViewController {
let titleArray = ["Troy and Abed", "Shirly and Annie", "Pierce and Ben"]
let noteArray = ["The best", "Pretty cool", "The worst"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// This is where I get the error
let cell = tableView.dequeueReusableCell(withIdentifier: K.reuseIdentifier, for: indexPath) as! NoteTableViewCell
cell.titleLabel?.text = titleArray[indexPath.row]
cell.textLabel?.text = noteArray[indexPath.row]
return cell
}
}
class NoteTableViewCell: UITableViewCell {
#IBOutlet weak var cardView: UIView!
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var mainTextLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
The full error text:
Could not cast value of type 'UITableViewCell' (0x1eccdbb70) to 'CollapseANote.NoteTableViewCell' (0x104d3ced8).
Could not cast value of type 'UITableViewCell' (0x1eccdbb70) to 'CollapseANote.NoteTableViewCell' (0x104d3ced8).
2021-02-18 10:36:47.613801-0500 CollapseANote[639:74162] Could not cast value of type 'UITableViewCell' (0x1eccdbb70) to 'CollapseANote.NoteTableViewCell' (0x104d3ced8).
Where I set the class for the prototype cell:
Where I set the class for TableViewController:
You nest to table cells and set the class name to the inner one that's why ist's not visible here
Drag the inner one to out like
Then delete the NoteItemCell
It's up to you to leave it if it represents another different cell

ios swift tableview not showing custom cells

I am trying to create a table view with custom cells from Storyboard layout in an iOS app.
But for some reason the table cells are not being shown. When I tried to set debug breakpoints I found that the debugger is reaching this function
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
but it never reaches this function -
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Here is my viewcontroller code -
extension NavigationViewController: UITableViewDataSource, UITableViewDelegate, SideMenuControllerDelegate {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SideMenuTableItem", for: indexPath as IndexPath) as! SideMenuTableItem
cell.setItemData(items[indexPath.row])
return cell
}
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func setupTableViews() {
menuTable.register(SideMenuTableItem.self, forCellReuseIdentifier: "SideMenuTableItem")
}
}
class SideMenuTableItem: UITableViewCell {
#IBOutlet weak var menuImage: UIImageView!
#IBOutlet weak var menuLabel: UILabel!
var data: MenuItem?
override func awakeFromNib() {
super.awakeFromNib()
}
func setItemData(_ item: MenuItem) {
data = item
menuLabel.text = data?.title
if data?.icon_res != nil {
menuImage.image = UIImage(named: (data?.icon_res)!)
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I have checked in the storyboard that I have set the reusable identifier to the table prototype cell and also connected the datasource and the delegate properties to the tableview
and I am calling the setupTableViews() method inside my viewDidLoad() function after creating the items array
But still I am not able to get the cells to appear in my view at all.
Can anyone suggest what am I missing here or what's wrong with my code, or how can I further debug this issue
import UIKit
import SideMenuSwift
class NavigationViewController: UIViewController {
#IBOutlet weak var navigationContainer: UIView!
#IBOutlet weak var emailButton: UIButton!
#IBOutlet weak var phoneButton: UIButton!
#IBOutlet weak var userAvatar: UIImageView!
#IBOutlet weak var userProfile: UIButton!
#IBOutlet weak var userName: UILabel!
#IBOutlet weak var menuTable: UITableView!
var service: AuthenticationService!
var cdc: CoreDataController!
var items: [MenuItem] = []
var currentUser: User?
override func viewDidLoad() {
super.viewDidLoad()
setupSidebar()
initSidebarData()
setupUserHeader()
setupTableViews()
}
func setupUserHeader() {
if currentUser != nil {
if currentUser?.name != nil {
userName.text = currentUser?.name
} else if currentUser?.role != nil {
userName.text = "urTutors " + (currentUser?.role ?? "")
}
if currentUser?.avatarUrl != nil {
userAvatar.downloaded(from: (currentUser?.avatarUrl)!)
}
}
}
func initSidebarData() {
service = AuthenticationServiceProvider()
cdc = CoreDataController()
items = cdc.getNavigationData()
currentUser = cdc.getUserData()
}
func setupSidebar() {
self.view.backgroundColor = UIColor.hexColor("#fff")
navigationContainer.backgroundColor = UIColor.hexColor("#2a2a2a")
SideMenuController.preferences.basic.statusBarBehavior = .hideOnMenu
SideMenuController.preferences.basic.position = .above
SideMenuController.preferences.basic.direction = .left
SideMenuController.preferences.basic.enablePanGesture = true
SideMenuController.preferences.basic.menuWidth = 275
sideMenuController?.delegate = self
}
static func createViewController() -> NavigationViewController {
let sb = UIStoryboard(name: "StudentHomeModuleStoryboard", bundle: nil)
let vc = sb.instantiateViewController(withIdentifier: "NavigationViewController")
return vc as! NavigationViewController
}
}
--UPDATE--
updated setupTableLayout function -
func setupTableViews() {
let bundle = Bundle(for: type(of: self))
let cellNib = UINib(nibName: "SideMenuTableItem", bundle: bundle)
menuTable.register(cellNib, forCellReuseIdentifier: "SideMenuTableItem")
menuTable.register(SideMenuTableItem.self, forCellReuseIdentifier: "SideMenuTableItem")
menuTable.reloadData()
}
After breaking into chat on this, we found that there were two issues.
The first issue was the missing reloadData call mentioned above. That was causing cellForRow to not be called. Adding reloadData corrected that issue, but then the custom cell class's outlets were nil, causing a crash in setItemData.
The second issue was that register(_:forCellReuseIdentifier:) was being called in code, but the custom cell was already setup as part of the Interface Builder UITableView declaration. Calling register again on the custom class re-registered the reuseIdentifier, disconnecting the outlets set up in the storyboard.
Removing the register call and adding reloadData solved all issues.
You are never calling setupTableViews(). You'r code should look like this:
class NavigationViewController: UIViewController, SideMenuControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
setupTableViews()
}
func setupTableViews() {
menuTable.reloadData()
}
}
extension NavigationViewController: UITableViewDataSource, UITableViewDelegate {
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SideMenuTableItem", for: indexPath as IndexPath) as! SideMenuTableItem
cell.setItemData(items[indexPath.row])
return cell
}
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
}
You are never calling the function, nor calling viewDidLoad. This should help. Also, where is the rest of your view controller code (is this all of it? It should not be!).
You don't need to register your cell because you requested it and make sure you reloadData().
Hope this helps!

Not work UITableView in cellForRowAt But numberOfRowsInsection Work in Swift

I wrote a function to add a Cell in a UITableView.
numberOfRowsInSection works but cellForRowAt does not work.
class ShopMenuViewCell: UITableViewCell {
#IBOutlet weak var menuTableView: UITableView!
var menuPrice: MenuPrice!
override func awakeFromNib()
{
super.awakeFromNib()
}
}
extension ShopMenuViewCell
{
func setView()
{
self.menuTableView.delegate = self
self.menuTableView.dataSource = self
self.menuTableView.regCells(cells: ["ShopMenuInformationCell"])
DispatchQueue.main.async
{
self.menuTableView.reloadData()
}
}
}
extension ShopMenuViewCell : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ShopMenuInformationCell", for: indexPath) as? ShopMenuInformationCell,
let menu = self.menuPrice,
let name = menu.name,
let price = menu.amt else { return ShopMenuInformationCell() }
return cell
}
}
In ShpMenuInformationCell.swift
class ShopMenuInformationCell: UITableViewCell
{
#IBOutlet weak var containerView: UIView!
#IBOutlet weak var shopNameLabel: UILabel!
#IBOutlet weak var menuNameLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
override func awakeFromNib()
{
super.awakeFromNib()
}
}
I saw the answer to add a ViewController, but I do not know what to do because it is not a storyboard. (.xib)
I think it is because I add UITableViewCell in the UITableViewCell class.
Do you know why?
I had a problem for about 5 days and found a solution!
First Reference Site: Joel's Answer cellForRowAtIndexPath: not called
Second reference site: Paolo's answer
Swift: UITableViewCell rowheight
When I add another kind of TableCell, the vertical size of the cell in question is small.
So I increased the size of the Cell's RowHeight (frame.height) before reloading the data and it's fixed!
Currently, cellforrowat runs safely. Your answers were helpful to the results.
Please try below code:
class ShopMenuViewCell: UITableViewCell
{
#IBOutlet weak var menuTableView: UITableView!
var menuPrice: MenuPrice!
override func awakeFromNib()
{
super.awakeFromNib()
}
fun setUpTable() {
self.menuTableView.regCells(cells: ["ShopMenuInformationCell"])
self.menuTableView.delegate = self
self.menuTableView.dataSource = self
self.menuTableView.reloadData()
}
}
and call the function setUpTable() from ShopMenuViewCell as an cell.setUpTable()
Try below code:
Solution 1
class ShopMenuViewCell: UITableViewCell {
#IBOutlet weak var menuTableView: UITableView! {
didSet {
self.menuTableView.regCells(cells: ["ShopMenuInformationCell"])
self.menuTableView.delegate = self
self.menuTableView.dataSource = self
}
}
}
Solution 2: Please check Anand Verma Answer

What can I do to access this Table View within a View Controller?

I have checked out the other posts but I'm not sure next steps here. Here's what I have currently. Do I need to add a tableView variable somewhere else? Connect something differently?
class LookbookPostViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = postArray[myIndex].title
// Do any additional setup after loading the view.
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
#IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
return postArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postViewCell", for: indexPath) as! LookbookPostViewCell
cell.postLabel.text = postArray[indexPath.row].title
print(postArray)
return cell
}
/
}
I then have this set up for my connections, but can't seem to get the Table View to recognize any data I'm trying to output.
I also have `class LookbookPostViewCell: UITableViewCell {
#IBOutlet weak var postLabel: UILabel!
}
`
I was able to solve this by changing:
class LookbookPostViewController: UIViewController
to
class LookbookPostViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
and then right clicking and making the delegate and datasource, the View Controller, like in this photo here.

Troubleshooting Custom UITableView Cell [Swift]

I have created a custom UITableView cell in my Storyboard that looks like this:
I hooked it up to my UITableViewCell class like this:
import UIKit
class StatusCell: UITableViewCell {
#IBOutlet weak var InstrumentImage: UIImageView!
#IBOutlet weak var InstrumentType: UILabel!
#IBOutlet weak var InstrumentValue: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Finally I attempted to initialize the UITableView from my UIViewController as such:
import UIKit
class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var TableView: UITableView!
let Items = ["Altitude","Distance","Groundspeed"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.Items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: StatusCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as StatusCell!
cell.InstrumentType?.text = Items[indexPath.row]
cell.InstrumentValue?.text = "150 Km"
cell.InstrumentImage?.image = UIImage(named: Items[indexPath.row])
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
However, when I attempt to run the program I get an EXC_BAD_INSTRUCTION error:
What could I have done wrong? Any help would be appreciated!
The debugger output shows that cell is nil, meaning it could not be instantiated. Furthermore, you are forcing an unwrapping of the optional (using the !) which causes the app to crash on the nil value.
Try to change your cellForRowAtIndexPath method like so (notice the dequeueReusableCellWithIdentifier method):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as StatusCell
cell.InstrumentType.text = Items[indexPath.row]
cell.InstrumentValue.text = "150 Km"
cell.InstrumentImage.image = UIImage(named: Items[indexPath.row])
return cell
}
Assuming that your custom tableViewCell class is correctly set up and the outlets are bound, there is no need to check for optionals.
Place a breakpoint on the let cell = ... line and step through the code. Check if cell gets initialized and is not nil.
And please: Do not use upper case names for properties and variables (your outlets, Items array...) as upper case names are for classes, structs, ...

Resources