I have the following situation:
There's a table view controller that displays all font family names, and it's inside a storyboard
Inside the UITableViewController in the storyboard there's a cell with identifier "FamilyName" that has also a disclosure indicator linked to a segue
I added a UISearchController that should filter the results of the previous table view controller
Inside the table view controller that acts as results updater I registered the same reuse identifier:
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "FamilyName")
And I'm calling dequeueReusableCellWithIdentifier:forIndexPath:, but I get a different UITableViewCell. I'd like to get the same cell that is in the storyboard inside the other table view controller, so that it's already linked to a segue.
Reuse your tableView. Don't make a new one just change the data source. This should work as intended. Just modify the data source and update the table.
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "FamilyName")
That means: Do not use the storyboard to get the cell. So you can hardly be surprised when the cell doesn't come from the storyboard: you said not to. The only way to get a cell from a storyboard is for the table view to register no class or nib for this cell reuse identifier.
Related
Very new to iOS dev so a little help would be greatly appreciated !
Created a table view controller with static cells in it. If no swift file is linked to it, I can see the cells when building the app.
However, if I link a table view controller to it (which I believe I should?), I don't see the cells anymore. Why is that?
Thanks !
There are several things you have to keep in mind when using Table View.
First, you have to make sure you link the table view to your file by control dragging it from the main.storyboard to your view controller.
Second, you must make sure that the cell has an identifier. Make sure you select the cell and not the table and set the identifier to a certain name (for simple projects, we generally just use the identifier "cell")
Third, you have to make sure that the datasource (and usually the delegate) is set to the view controller.
There are two ways of doing that, first, you can drag the table view to the View Controller icon (I believe it is the yellow one, but one of those on the top of your iOS screen storyboard), and select datasource and delegate. The second way is after you have referenced your table view to your view controller file, in the viewDidLoad method, set the table view's delegate and datasource to self. For instance, if your table view is named as tableView, the code should look something like
tableView.delegate = self
tableView.dataSource = self
Forth, make sure you include the following methods in your class. The numberOfRows, cellForRowAtIndexPath, and for numberOfSections, just return 1.
If everything still doesn't work, you can share us the code, maybe there are some other potential problems I forgot.
I have two TableViewControllers, and the layout of two prototype cells are the same. Is it possible to extract the layout so that I don't need to maintain multiple copies?
You can move those prototype cells into a xib file and register the UINib on each tableview.
use the cell via xib and you can reuse for the other table
To create xib right click on project on xcode choose new file then choose UserInterface choose view from it and save the name
then for the class of the xib file five your appropriate table view cell class and create outlet of the controls inside table view cell
to load the nib file on table view controller declare these two lines on your table view controller viewdidload function
let nib = UINib(nibName: "your nib file name",bundle: nil)
erxTable.registerNib(nib, forCellReuseIdentifier: "identifiername")
Thanks,
If you drag a UITableViewCell onto the top toolbar of a view controller, it appears above the view controller in IB as a secondary view. This is nice because you can do all the layout there. But how do you then get a table view to load the cell from there?
This doesn't work:
[self.tableView registerClass:[MyCustomTableViewCell class] forCellReuseIdentifier:#"MyCell"];
And since it's not in a separate nib file, registerNib doesn't seem appropriate either. Any ideas?
Although that may be possible you have several options when designing tableview cells. You can either design it in a separate .xib file or you can use a prototype cell. Below is an example of a separate .xib file. When you use a separate .xib you would use the registerNib method.
Or with a prototype cell in which the cell is automatically registered with the tableView.
I have a tableview that has a custom UItableview cell which I defined in the Main.storyboard file. When I use Search Display Controller, I need to register the class with the searchDisplayController's tableView inside viewDidLoad. The custom table view cell is called "LargePostTableViewCell".
self.searchDisplayController!.searchResultsTableView.registerClass(LargePostTableViewCell.classForCoder(), forCellReuseIdentifier: LargePostTableViewCell)
However, I also need to register the nib file that is associated with LargePostTableViewCell, but that is defined in Main.storyboard. In all the examples I saw, people defined separate nib files for the custom table view cell and was able to reference it that way. Is is possible to reference a UINib for a custom table view cell that's inside Main.storyboard? Here is my code for trying to register the nib file:
self.searchDisplayController!.searchResultsTableView.registerNib(UINib(nibName: "LargePostTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "LargePostTableViewCell")
I have a UITableViewController subclass in a Storyboard which loads it's cells from a UITableViewCell defined in a XIB file (this is done so that a custom cell can be re-used across a deep hierarchy of table view controllers).
I need to define the segue from this table view controller to the next table view controller. Normally you would do this by control dragging from the cell of the source to the view controller of the destination. But since there are no prototype cells in the source table view controller, there is no way to connect the segue.
Do I have to resort to implementing -tableView:didSelectRowAtIndexPath: in my table view controller and triggering a manual segue? Or is there a better way?
Yes, present the new UIViewController from -tableView:didSelectRowAtIndexPath:. Since you don't have a prototype cell, you have nothing to link the segue with. Just -presentViewController:animated:.