dequeueReusableCellWithIdentifier doesn't return cell - ios

I have a static table with one static section. Other sections are dynamic.
I create Table Section and Table Cell for dynamic section. Set identifier for Cell, set custom class for it and even do:
self.tableView.registerClass(UncheckedStoreTableViewCell.self, forCellReuseIdentifier: "StoreCell")
if i don't register it with code, then i get:
'unable to dequeue a cell with identifier StoreCell - must register a
nib or a class for the identifier or connect a prototype cell in a
storyboard'
So when i use this:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0 {
return super.tableView(tableView, cellForRowAtIndexPath: indexPath)
}
let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as! UncheckedStoreTableViewCell
return cell
}
It works. But if i'm trying to change label: cell.myLabel.text = "one"
or just print(cell.myLabel) got
BAD_INSTRUCTION

You can definitely use dynamic cells in a static table view.
Don't expect a static table view to register your cell's identifier for you. Just do it yourself.
Do you have outlets in the cell class to some view in interface builder? If I were you I wouldn't expect the table view to know about that. It will instantiate your cell class, and that's it. No outlets will be set. I think this is related: load nib in view subclass
By the way, if you've defined a custom .nib for your cell, there's this method: registerNib(_:forCellReuseIdentifier:)

You do not need to register your cell in code.
You have correctly set the identifier of the cell, however this is not enough. In addition to this you also need to open Identity Inspector for your cell and set the class of the cell to be UncheckedStoreTableViewCell. Here is an image showing you where you should set it:
Without this step Xcode will not be able to correctly associate your cell identifier with your custom cell as it doesn't know anything about it!

Related

Proper way to identify static cells in Xcode?

I have a table view controller and I overrode this function:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
// Configure the cell...
return cell
}
My cell identifier is myCell. I created 4 static cells, and went into each cell and gave each the identifier myCell. However, this crashes due to not recognizing the cell identifier. However, if I change to dynamic prototypes, give the cell the myCell identifier, it works as intended. I guess this will do for now...as I can just tell it dynamically how many cells I want. But I'm really interested to find out why my static cells aren't working with the same exact method. Any ideas?
When you have static cells (i.e. a small, fixed set of static cells, rather than dynamic cells with cell prototypes where you control how many of which types of cells will be dynamically generated), you shouldn’t implement any of the UITableViewDataSource methods. Just create IBOutlet references for the various controls you have in your static cells and update them just like you would if you weren’t using a UITableView at all.
As an aside, if you were using dynamic cells, you would never give two different cell prototypes the same reuse identifier. The purpose of the reuse identifier is to let it know which cell prototype to use, and it wouldn’t therefore make any sense to give multiple cell prototypes the same identifier.

Swift: Searching the object from the memory address [duplicate]

I usually never have a problem with this step, but for some reason when I re set up my storyboard now I have an issue with this part of my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
(cell as! TableViewCell).usernameLabel.text = friendsArray[indexPath.row] as String
return cell
}
I have done all of the obvious stuff like making sure the cell identifier is "Cell".
My error is this:
Could not cast value of type 'UITableViewCell' (0x1094ada18) to 'YOpub.TableViewCell' (0x106620410).
EDIT: Problem was I had self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") in my didLoad function. After removing this line my code worked again.
You may have forgotten to tell the storyboard that this cell is a TableViewCell. Select the prototype cell and set the Custom Class in the Identity inspector.
You may have called registerClass:forCellReuseIdentifier:. If so, delete that call; it actually prevents us from getting the cell from the storyboard.
You are calling tableView.dequeueReusableCellWithIdentifier. That is a big mistake. You should call tableView.dequeueReusableCellWithIdentifier:forIndexPath:.
One of the reason,May be in Identity inspector module is different then your project name .

Swift custom table cell

I've been trying to use Custom Table cell in my swift app, and faced a problem: My application crashes when I run my app.
My custom table cell class is SimpleTableCell (Objective-C class)
and my cellForRowAtIndexPath method (swift) is:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("SimpleTableItem", forIndexPath: indexPath)
let data = TableData[indexPath.row]
cell.textLabel?.text = data.description
if (data.image == nil)
{
cell.imageView?.image = UIImage(named:"image.jpg")
load_image(image_base_url + data.imageurl!, imageview: cell.imageView!, index: indexPath.row)
}
else
{
cell.imageView?.image = TableData[indexPath.row].image
}
return cell
}
My app crashes with the error:
unable to dequeue a cell with identifier SimpleTableItem - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
What does it mean?
In the storyboard, select your tableView cell and in the attributes inspector, set the identifier to "SimpleTableItem". Hope this helps
dequeueReusableCellWithIdentifier is trying to find a cell with the identifier "SimpleTableItem", but it seems that you haven't given a cell that identifier anywhere. One way to do this is to choose a cell in the Table View in Storyboard, go to the Attributes Inspector and set its identifier to SimpleTableItem.

swift custom UITableViewCell with TextView inside disappears

I am trying to get a custom tableView cell with a textView inside working in my tableView. I have made a custom UITableViewCell with a textView inside it.
I can get the custom made UITableViewCell with the textView inside to appear in the UITableView.
I can click inside the textView to type something, but when I finish typing and click on another tableViewCell, the first tableViewCell with the textView inside disappears. After disappearing, it becomes an empty tableViewCell. XCode gives this message:
"no index path for table cell being reused"
However, when I scroll away in the tableView and scroll back to the empty tableViewCell, it reappears.
I don't know how to keep the tableViewCell from disappearing. It seems like the answer has something to do with using the restorationIdentifier inside of UITableView, but I'm not sure how to use it. In the docs, it says to use restorationIdentifier for state preservation.
Here is the relevant code I have:
inside ViewDidLoad():
tableView.registerClass(PhotoAndRateTableViewCell.classForCoder(), forCellReuseIdentifier: ReuseIds.reviewCell)
tableView.registerNib(UINib(nibName: "PhotoAndRateTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: ReuseIds.reviewCell)
inside cellForRowAtIndexPath:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var section = indexPath.section
let cell = UITableViewCell()
var cell = tableView.dequeueReusableCellWithIdentifier(ReuseIds.reviewCell, forIndexPath: indexPath) as PhotoAndRateTableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
You're misusing the dequeue procedure:
var cell = tableView.dequeueReusableCellWithIdentifier(ReuseIds.reviewCell, forIndexPath: indexPath) as? PhotoAndRateTableViewCell
if (cell == nil) {
cell = PhotoAndRateTableviewCell();
}
You want to reuse a cell if available, or create a new one if not. In your case, you're creating a cell every time (of the generic class) and then attempting to dequeue a cell from your custom class (which has never been created)
As far as preserving the data, you need to implement the prepareForReuse method in the table cell which should clear whatever index specific data was contained in the cell. Then in cellForRow you can re-set the data for the cell for re-appearance

Can I use a single prototype cell in multiple tableViews?

I am having two different tableviews in two different controller. But the cells, that I need to display in them, look identical. I have created a prototype cell in one tableView and subclassed UiTableViewCell. Now, if I want to use the same cell in a different controller, how can I use it ?
If I just import that customCell file in the new controller and deque it using the same identifier given in the storyboard, it wont work. It says
Assertion failure in -[UITableView
_configureCellForDisplay:forIndexPath:]
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'UITableView dataSource
must return a cell from tableView:cellForRowAtIndexPath:'
So, clearly it means , the cell is nil. So how can I instantiate the same cell from the storyboard ? Is it possible or do I have to create a different customCell for the new table too ?
K.. I got it. First of all,
I can NOT use one prototype cell in two different tableview. But, I
can use the same tableViewCell subclass in two different tableviews.
To achieve it, one just have to copy the prototype cell from one controller and paste it as a prototype cell of the other tableview. Class of the pasted tableview remains the same. Just change the reuseIdentifier. and use it.
Edit:
If your cell has a fairly complicated UI, then it makes more sense to create separate xib for the cell alone. Then programmatically register the xib with the table view. That way, you will have only one copy of the cell and much better at maintaining it when there are changes to the ui.
You can use same prototype cell in different view controllers, you just need to dequeue it from the tableview of controller in which you designed it.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let viewControllerInWhichCellWasDefined = tabBarController?.viewControllers?[0]
let cell = viewControllerInWhichCellWasDefined.tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath)
return cell
}
If you create a custom cell in XIB it should work just fine. However, I suspect the cell's identifier caused the problem. Try to change your cell's identifier for each table view controller.
If it's not, you might want to post the source code
Yes. We can use a prototype cell of ViewControllerA's tableview for tableview of ViewControllerB. Just need to implement the following code in ViewControllerB
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let vc_array = self.navigationController?.viewControllers
let view_controller = vc_array![vc_array!.count - 2] as! ViewControllerA
let cell = view_controller.tableView.dequeueReusableCell(withIdentifier: "ViewControllerA_ID", for: indexPath) as! ViewControllerA
return cell
}
Here ViewControllerA is the ViewController which contains the tableview with prototype cell, and we are using the same cell on the tableview of ViewControllerB
I have used the same UITableViewCell in different apps. just copy one TableViewCell to the other app. That is both apps have the same layout and such. The apps don't belong to the same workspace. The setup works nice, no problems or errors.. just don't know if thats good practice.

Resources