The program is building successfully, but when it goes into the simulation, it crashes, and the fatal error message is logged to console.
The line that triggers the error is:
let cell = self.tableview?.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
Is cell returning nil? You need to make sure that you're passing the correct identifier of the UITableViewCell in your storyboard to the withIdentifier: "cell" parameter. Also, what is being logged to the console when you get this error? Many times it will point to the solution.
Give same identifier "cell" in storyboard.
Related
I am new to the iOS programming scene and I recently came across some code examples online of implementations like:
functableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: customCellIdentifier, for: indexPath) as? CustomCell
if (cell == nil) {
cell = CustomCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: customCellIdentifier) asCustomCell
}
...
}
Where the author tried to handle the event where dequeueReusableCell return nil.
But from my limited personal experience with UITableView and custom cells, I have yet to encounter a time when dequeueReusableCell returned me nil.
From researching, I found the reason could be
"The dequeue… methods try to find a cell with the given reuse
identifier that is currently offscreen. If they find one, they return
that cell, otherwise they return nil."
from MrTJ's answer here
But that has never happened once to me. When I purposely give it a wrong identifier, a runtime error would occur but not once was nil returned. I wonder when exactly that would happen and if handling it is really necessary.
That code isn't correct. The older form of dequeueReusableCell without the indexPath parameter returns nil when there isn't an available cell in the reuse pool (i.e. when the table view first appears). In that case it is your responsibility to allocate a cell.
The newer form of dequeueResuableCell, shown in your question, will always return a cell as it allocates a cell if required.
The expression in your question can return nil if the conditional downcast fails (that is, the cell that was returned wasn't an instance of CustomCell).
I would argue that this represents a serious misconfiguration somewhere and should be found during development. For this reason a forced downcast is normally used; during development you get a crash, fix the problem and move on.
let cell = tableView.dequeueReusableCell(withIdentifier: customCellIdentifier, for: indexPath) as! CustomCell
The code in your question is some sort of Frankenstein mixture of the old and the new.
I have a TableVC which has categories when you tap on one it takes you to the items within it. But when I do this I get the following grouping of multiple errors when the app crashes:
1.
error initializing newrealm, Error Domain=io.realm Code=1 "Provided
schema version 0 is less than last set version 1."
UserInfo={NSLocalizedDescription=Provided schema version 0 is less
than last set version 1., Error Code=1}
2.
2018-08-01 12:56:06.152225-0400 Todoey[35380:4690261] Unknown class
SwipeTableViewCell in Interface Builder file.
3.
Could not cast value of type 'UITableViewCell' (0x1096e3580) to
'SwipeCellKit.SwipeTableViewCell' (0x106d231d0).
4.
2018-08-01 12:56:06.153831-0400 Todoey[35380:4690261] Could not cast
value of type 'UITableViewCell' (0x1096e3580) to
'SwipeCellKit.SwipeTableViewCell' (0x106d231d0).
I have followed the steps laid out in many previous questions but no luck
This is where I get a -Thread 1: signal SIGABRT-
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SwipeTableViewCell
cell.delegate = self
return cell
}
Help please
all you have to do, select your tableViewCell -> go to identity Inspector -> select the custom class as SwipeTableViewCell and module as SwipeCellKit. then run...
I think you are overriding the registration of cell to your table view, with the method:
#available(iOS 6.0, *)
open func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)
and somewhere in viewDidLoad, or viewDidAppear, or viewWillAppear you registered to tableView a simple UITableCell.self instead a custom cell, as bellow :
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
In case you create the SwipeTableViewCell programatically use this method in viewDidLoad as bellow:
self.tableView.register(SwipeTableViewCell.self, forCellReuseIdentifier: "Cell")
In case your SwipeTableViewCell is created in storyboards, I don't recommend to use this method, because you override the constructor, and your objects inside the SwipeTableViewCell need to be initialized programatically. Just remove the registration line, because in the storyboard you have registered the custom cell. Take care to have set identifier for cell.
Although it is not in the question, I encountered the same problem on the same tutorial, so same app. This might help some future passersby.
It should work with:
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) as! SwipeTableViewCell
It WILL NOT work with:
let cell = UITableViewCell(style: .default, reuseIdentifier: "CategoryCell") as! SwipeTableViewCell
How do I change the following closure so that cell is 'weak'? :
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as? PlayerTableViewCell else {
fatalError("The dequeued cell is not an instance of PlayerTableViewCell")
}
I am sure there is a simple way to achieve this but I have been unable to determine the correct way to handle this.
Thanks
A duplicate call to dequeueReusableCell was causing a memory consumption problem, which led me to think I had a strong reference that needed to be weakened.
Removing this erroneous extra call solved the underlying issue I thought needed a fix.
I am trying to add a custom cell to tableview in Swift3 but getting a strange error. Here is the screenshot of error.
Write indexPath in place of IndexPath like this:
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
Also, make sure that CustomCell is subclass of UITableViewCell.
Replace the placeholder IndexPath with lowercase indexPath
Placeholders – with a light blue background – are tokens which indicate the expected types of the parameters.
But you got more serious issues than that error.
Your CustomCell class does not inherit from UITableViewCell. Change it to:
class CustomCell:UITableViewCell {
// your custom cell implementation
}
I am getting an error Thread1: signal SIGABRT in the line below, I already tried:
Product>Clean, Restart Computer
check all connections to the storyboard
check if some connection is lost
the Identifier is set to Cell in the attribute inspector
plus look into many posts in this forum.
After all this I deleted all connections with storyboard and connect again but still get the same error :(
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell2 = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! DefinitionTableViewCell
In the console:
Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-3347.44/UITableView.m:6245
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell2 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Any help is more than welcome Tks :)
Well, as the exception message suggests, you "must register a nib or a class for the identifier". Try adding this line in viewDidLoad():
tableView.registerNib(UINib(nibName: "nameOfYourCustomNib", bundle: nil), forCellReuseIdentifier: "cell2")
Replace the nameOfYourCustomNib with your nib name, for example if you have a file MyCustomCell.xib then the name would be MyCustomCell.
If this fails, try registering a class instead of a nib (remember to delete the first line), by typing:
tableView.registerClass(DefinitionTableViewCell.self, forCellReuseIdentifier: "cell2")
And the most important thing, in cellForRowAtIndexPath, make sure you check if the cell is nil, and if so, create a new DefinitionTableViewCell.