Unable to dequeue a cell with identifier memoCell error - ios

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row=animals[indexPath.row]
let cellIdentifier = "memoCell"
let cell=tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MemoCellTableViewCell
print(cell)
print("mmmmmmm")
print(cell.subject)
cell.name?.text="aaa"
return cell
}
I'm unable to set value to label, because I have an error saying:
'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
So, I added tableView.register(MemoCellTableViewCell.self, forCellReuseIdentifier: "memoCell"). Then, the problem is that I can't set a value to cell.name because cell.name is nil.

First check that your outlet to the cell.name is connected perfectly then you can try by updating your method in below manner,
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row=animals[indexPath.row]
let cellIdentifier = "memoCell"
var cell=tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MemoCellTableViewCell
if cell == nil {
cell = MemoCellTableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}
print(cell)
print("mmmmmmm")
print(cell.subject)
cell.name?.text="aaa"
return cell
}

First of all remove this methods
tableView.register(MemoCellTableViewCell.self, forCellReuseIdentifier: "memoCell")
because your cell is in storyboard so you don't require this method, if you are using tableviewCell XIB then you can use this.
And in your MemoCellTableViewCell.swift file, create or connect all require outlet using storyboard and then try again. It should resolve issue, otherwise let me know.

Related

Swift Custom Tableview Cell with UITableViewCellStyle

I am trying to make a custom table view cell.
If I do this:
class TableViewCell: UITableViewCell {
#IBOutlet weak var cellBackgroundImage : UIImageView!
}
And:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
cell.cellBackgroundImage.backgroundColor = UIColor.white
cell.cellBackgroundImage.layer.masksToBounds = false
cell.cellBackgroundImage.layer.cornerRadius = 5
let event = self.fetchedResultsController.object(at: indexPath)
self.configureCell(cell, withEvent: event)
return cell
}
I obtain a white rounded cell background. Easy. And I can use the original cell.textLabel.text.
Perfect.
But, if I want to do something more complex:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? TableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell") as? TableViewCell
cell?.cellBackgroundImage.backgroundColor = UIColor.white
cell?.cellBackgroundImage.layer.masksToBounds = false
cell?.cellBackgroundImage.layer.cornerRadius = 5
self.configureCell(cell!, withObject: object)
}
And at the same time to use the original table view properties as UITableViewCellStyle.subtitle and cell.accessoryView, the app crashes or shows the wrong output.
THIS MEANS THAT I MUST USE A FULL CUSTOM CELL WITH MORE OUTLETS TO REPLACE THE ORIGINAL ELEMENTS AS UITableViewCellStyle.subtitle and cell.accessoryView ???
I will express it in a different way:
Can I use a custom tableview cell only for one purpose (like the rounded background) and use the original elements such as the subtitle style and the accesory view?
In afirmative case, how?

reloadData for UITableView changes constraints

I have a UITableView set up with cells: (no conflicting constraints)
Storyboard and Constraints
Looks like:
Pre-refresh
However, whenever I refresh using
self.tableView.reloadData()
the constraints seem to change which changes the cell into:
Post-refresh
Any suggestions as to why this happens?
Code for cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CaptureCell
let capture = tabBar?.captureArray[indexPath.row]
cell.capsuleImage.image = capture?.image
return cell
}
The cell identifier is "cell" and I have CaptureCell which is a subclass of UITableViewCell.

ios swift making table list keep getting error about nill

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier") as! CustomTableViewCell
let text = data[indexPath.row]
cell.label.text = text
return cell
}
above on is the code that I saw to follow.
my code is as below
I don't know why it getting nil value on
tableView.dequeueReusableCell(withIdentifier: "locCell")
my storyboard is as below
I added identifier like below(you can see it on bottom-right section of pic
You need to register the cell for reuse.
tableView.register(LocationTableCell.self, forCellReuseIdentifier: "locCell")
Or enter your reuse identifier in the storyboard by selecting your cell and then entering the reuse identifier in the properties to the right.
Simply because tableView.dequeueReusableCell(withIdentifier: "cell") by default is nil.
It is the same case for any optional when trying to print it out, example:
let optionalString: String? = ""
print(optionalString)
leads to get:
So, by declaring a constant as:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
since dequeueReusableCell(withIdentifier:) returns an optional UITableViewCell instance, the type of cell would be UITableViewCell? (optional UITableViewCell), that's why you are seeing this error.
How to get rid of it?
Assuming that you have set the cell right identifier for your cell:
Well, in case of having your custom cell, you could cast it as:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? MyCustomCell else {
// something goes wrong
return UITableViewCell()
}
print(cell) // it would be fine for now
// ...
return cell
}
And if you don't have a custom cell, all you have to do is to remove the as? MyCustomCell down casting.
Replace
let cell = tableView.dequeueReusableCell(withIdentifier: "locCell")
With this code:
let cell = tableView.dequeueReusableCell(withIdentifier: "locCell", for: indexPath)
func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier", for: indexPath) as! CustomTableViewCell
cell.label.text = data[indexPath.row].name
return cell
}
NOTE: In your storyboard set tableView Delegate, DataSource and set cell ID cellReuseIdentifier.

It crashes in cellforrowatindexpath of tableview , says : "fatal error: unexpectedly found nil while unwrapping an Optional value"

I have two tableview on the same Collectionviewcell. When I run the application, it crashes in cellforrowatindexpath of tableview , says : "fatal error: unexpectedly found nil while unwrapping an Optional value"
Please find below some of my code -
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
reuseIdentifier, for: indexPathOfCollectionView as IndexPath) as!
MyCustomCollectionViewCell
if(tableView == cell.graphTableView){
let cell:CustomGraphTableViewCell =
tableView.dequeueReusableCell(withIdentifier: "CustomGraphTableViewCell") as!
CustomGraphTableViewCell
return cell
}
else
{
let cell:MyCustomTableViewCell =
tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell") as!
MyCustomTableViewCell
cell.nameLabel.text = namesArray[indexPath.row]
return cell
}
}
Can anyone please suggest the solution. Why is it happening and what can be the solution for the same?
In tableview UICollectionViewCell won't work, UICollectionViewCell is different class which is inherited from UICollectionReusableView and UITableViewCell is a different class from UICollectionViewCell and it is inherited from UIView, NSCoding, UIGestureRecognizerDelegate, so here, you have to use only uitableview cell for your tableview. Else it will crash.
When your tableview delegate method return some value not but null or 0, then datasource method of tableview will execute, that means when your tableview's delegate method have some value the
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell
above method will now started executing and when it found
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
reuseIdentifier, for: indexPathOfCollectionView as IndexPath) as!
MyCustomCollectionViewCell
above statement, it will get crashed.
This is
let cell:MyCustomTableViewCell =
tableView.dequeueReusableCell(withIdentifier: "MyCustomTableViewCell") as!
MyCustomTableViewCell
cell.nameLabel.text = namesArray[indexPath.row]
return cell
only put this statement under the cellforRow datasource method, it will work accordingly.
Thanks
Please check this line
cell.nameLabel.text = namesArray[indexPath.row]
The cell.nameLabel is always accept string value, first convert into string value like this:
cell.nameLabel.text = namesArray[indexPath.row] as NSString

SIGABRT on UITableView

I have this code, that I copied out of the default MasterDetail App, yet when I run the program, it stalls on the line with the declaration of the cell. And I have no clue why.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let object = objects[indexPath.row].title as String
cell.textLabel.text = object
return cell
}
You haven't register cell to use on table view
I hope you forgot to do this.
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
If you didn't register the cell for tableview, then it may return nil to cell.
Apple Says
let == Constant
var == variable values at any time
So using ? will consider as optional
you should have like this
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
//OR
//var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
//Your stuff
return cell
}
According to This tutorial from Apple, you may want to check the Table View on the story board.
Look for the attribute inspector for the Table View and verify that the "content" field is set to "Dynamic Prototypes". Updating this fixed it for me.
Also, while you have the hood open, verify that there is a single table cell in the table view, and make sure that it's "Identifier" field is set to the value you had in the tableView.dequeueReusableCellWithIdentifier method call (in your case "cell")

Resources