Swift: Iboutlets Are Nil in Custom Cell

IBoutlet is nil in my custom table cell?

There are two variants to register, but both take a parameter called
forCellReuseIdentifier, which is a string that lets you register
different kinds of table view cells. For example, you might have a
reuse identifier "DefaultCell", another one called "Heading cell",
another one "CellWithTextField", and so on. Re-using different cells
this way helps save system resources.

If you want to use register() with a Swift class, you provide a table
view cell class
as its first parameter. This is useful if your cell is
defined entirely in code
. As an example, this uses the default
UITableViewCell class:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")

You can then dequeue that cell like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultCell")!
return cell
}

The other option is to use register() with an Interface Builder nib
file
. Nibs contain the class name to use along with their design, so
this method is more common
. Here's an example

tableView.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "CellFromNib")

But if you're using storyboards you will find it easier to create
prototype cells and give them a reuse identifier directly inside
Interface Builder.So no need to register programmatically.

Remove this line form viewDidLoad

tableView.register(CardTableViewCell.self, forCellReuseIdentifier: "cardCell). 

Swift: IBoutlets are nil in Custom Cell

The problem is this line:

self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell")

Delete it. That line says: "Do not get the cell from the storyboard." But you do want to get the cell from the storyboard.

(Make sure, however, that the cell's identifier is "TimesheetCell" in the storyboard, or you'll crash.)

UITableView custom cell IBOutlets are nil

You need to register your Custom Xib file in your ViewController first.

From Apple Docs on registerClass:forCellReuseIdentifier::

call this method or the registerClass:forCellReuseIdentifier: method
to tell the table view how to create new cells. If a cell of the
specified type is not currently in a reuse queue, the table view uses
the provided information to create a new cell object automatically.

E.g. Write this line in your viewDidLoad, after setting tableView delegate and dataSource:

[yourTableView registerNib:[UINib nibWithNibName:@"ChatCustomCell" bundle:nil] forCellReuseIdentifier:@"ChatCell"];

If you don't, your TableView won't be able to load the Cell Nib file, which would result in nil IBOutlets of Cell.

Custom table view cell: IBOutlet label is nil

First off, you're using a nib file to load your custom cell into the table. That's probably going to be more of a headache than it's worth if you're new to Swift/Cocoa. I would move everything over to storyboard for the time being. Instead of using a nib file click, go to Storyboard, click on your UITableView and make sure the TableView's content setting is Dyanamic Prototypes:

Sample Image

Next, click on the prototype cell (the only one in the table view) and set the class to CustomTableViewCell and set its reuse identifier to customCell:

Sample Image
Sample Image

Next, add a label to your prototype cell and link it to the IBOutlet in your CustomTableViewCell class. You don't need to register your customCell so long as you've set the reuse identifier in storyboard. Delete this line:

self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")

and it should run.

UITableViewCell IBOutlets are nil

in the storyboard select UITableViewCell prototype , in attributes inspector set "BasicCell" as identifier.

right click on UIImageView And UILabel in Cell and make sure there is no unLinked or old connection.

remove self.tableView.registerClass(..)

it should be fine.

IBOutlet Nil in UITableViewCell .Xib file

Your inside view did not connected with your file owner, so make a IBOutlet property.

@IBOutlet private var contentView: UIView!

connect this with the main view of xib file as a refrence outlet.

and inside this function of your custom class

@objc required public init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! commonInit() // write this to load nib file }

in commonInit() load your nib from bundle and add your contentview as commonInit()

Swift 2.1 - IBOutlet property is nil in custom UITableViewCell class

The cell's outlets are not instantiated at the point when you try to access them in configureSelectedCategoryCell.

As I understand the cells are designed in Interface Builder, so the outlets of the cell object cell are missing the references to the controls in Interface Builder.

After a category is selected you should

  1. Update the tableView's data source
  2. Reload the tableView's items

Something like this:

func categoryController(controller: RecipeCategoryTableVC, didSelectCategory category: String) {

selectedCategory = category
self.navigationController?.popViewControllerAnimated(true)

tableViewDataSource[selectedItemIndex].selectedCategory = selectedCategory
tableView.reloadData()
}

Then in the table view data source delegate:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifer") as! RecipeCategoryCell
cell.configureSelectedCategoryCell(tableViewDataSource[indexPath.row].selectedCategory)
return cell
}

Side notes:

It is legit to connect one IBOutlet to multiple controls in the storyboard.

As precaution I suggest to remove the force unwrapping of an optional in configureCell, e.g.

func configureCell(recipe: Recipe) {
if let category = recipe.category as? RecipeCategory {
recipeCategoryLabel.text = category.name
}
}


Related Topics



Leave a reply



Submit