Im trying to have 2 tableview in a single view controller with one tableview being static while
the other dynamic.
My view controller is set up like so
The top half being the static tableview.
i created ibOutlets for both tableViews but i can't seem to be able to customise the tables.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as UITableViewCell
//tableview2 is the dynamic tableView.
if (tableView == self.tableView2){
print("Tableview2")
}
else{
println("HELLLO")
}
return cell
}
i get the error * Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:6116
2014-10-23 22:31:52.246 Recipe app[2857:504809] * 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'.
As error says you "must register a nib or a class for the identifier or connect a prototype cell in a storyboard".
Do this on viewDidLoad or awakeFromNib.
self.tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier:"cell2")
Or you have to make prototype cells in storyboard and give it "cell2" identifier
Assuming you have given the prototype cells for your dynamic table the reuseIdentifier "cell2", move this line:
let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as UITableViewCell
into either the if ... clause, or the else ... clause (whichever deals with the dynamic table). If tableView is the static table, it throws the error.
Related
So I'm trying to connect my custom table view cell into a table view in my main VC. The cell has its own xib file and its own swift file. I originally had the cell set up as a custom prototype cell in the table.
So the problem I have is in connecting my new custom cell to the viewcontroller. The code I have now is:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell")! as! CustomTableViewCell
return cell
}
Of course this set up is not working. The error I get during runtime is: "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" which I assume is referring to "cell" identifier. So my question is, can I connect my cell to this function, or do I need a new one to initiate and return "cell" to this table view?
You need to register the cell with the UITableView before you can use it. In your viewDidLoad method you need something like the following.
let nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "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.
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!
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
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.