How UITableView working without registerClass:forCellReuseIdentifier: - ios

In stroryboard I have UIViewController which contains UITableView and inside that I created a dynamic UITableViewCell and give it an identifier.
In UIViewController in cellforrowatindexpath method I simply did [tableView dequeueReusableCellWithIdentifier:CellIdentifier].
I did not use registerClass:forCellReuseIdentifier: but my app is working perfectly.
How without registerClass:forCellReuseIdentifier: app is working and cells are dequeueing perfectly?

The cell definition for that identifier is specified in the storyboard, during compilation this is converted into an NIB and during unarchiving the cell NIB is registered with the table view.

registerClass:forCellReuseIdentifier:
This method is only used when you are using custom class for your cell in tableview, currently you have defined a prototype cell which automatically uses default UITableViewCell class, and you have to access the cell properties with tag value, but in case of custom class you make property or outlet for the labels/textviews/imageviews in CustomCell.h and you have to register the Class with a unique Identifier name at time of loading the ViewController, so that tableview can understand at runtime which cell class should be loaded as per the cell identifier used in storyboard.

Discussion
Before dequeueing any cells, call this method or the
registerClass:forCellReuseIdentifier: method to tell the table view
how to create new cells. If a cell of the specified type is not
currently in a reuse queue, the table view uses the provided
information to create a new cell object automatically.
If you previously registered a class or nib file with the same reuse
identifier, the nib you specify in the nib parameter replaces the old
entry. You may specify nil for nib if you want to unregister the nib
from the specified reuse identifier.
If You are using separate UITableViewCell class or xib file for UITableviewCell ContentView, where the UIElements are access through the class file. In Storyboard the UITableViewDataSource get the cell by its identifier.
UINib *sectionHeaderNib = [UINib nibWithNibName:#"Header" bundle:nil];
[self.tableView registerNib:sectionHeaderNib forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];
// [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"]; // register your your UITableView Class
Check the LazyTableImages Project
and UITableView Class Reference

Related

Populate data to UIcollectionview cell created with xib

I have create my custom collectionview cell with its own class. And I will register them programmatically on a UIviewcontroller class.
The problem I facing now is, I will do all the API call on the UIviewcontroller class but how to I populate them on all the cells I want, because each cell class also will have their own data source and delegate extension.
Note: There is a one cell class inside contain another collectionview.
Thanks.

UITableView - how to register a cell from secondary view?

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.

Programmatically register a nib from a custom table view cell defined in main storyboard

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")

Can't assign custom UITableViewCell within custom UIViewController

In my storyboard, if I have a UIViewController that inherits from a custom class, can I create a custom class for a UITableViewCell within a tableview inside that UIViewController?
I've tried to do the above, but when I try to connect outlets, I can only drag to the parent ViewController file, and not to the connected TableViewCell file. Why is that?
You can connect outlets of your cell.
You need to assign the class of custom table view cell in storyboard.
Select the table view cell while dragging to connect outlets.
You seem to be asking 2 questions:
"can I create a custom class for a UITableViewCell"?
Yes, you can use a custom class for your UITableViewCell. Simply select the cell and in the class inspector, enter the custom class.
"I can only drag to the parent ViewController"
That's also correct. For static cells, you can link them to outlets in the view controller. Note that for dynamic cells, you'll need to create them in the tableView:cellForRowAtIndexPath: method.

UITableView inside UITableViewCell using Interface Buillder

I have an tableView called BasicTable that have custom cell called CustomCell and I want to add a new table view inside this CustomCell.
I have an BasicTableView.m for BasicTable and CustomCell.m for CustomCell and add the new table view using Interface Builder but now don't know where to go or where to add reference to the new tableView and it's new cell.
You can create a reference to the new tableView directly in CustomCell.m (e.g. by setting it as a property) and set the cell itself als dataSource and delegate of the tableView.
Having another tableView inside of a table view cell however doesn't seem like a good design decision, this will lead to problems in UX (e.g. concerning the scrolling of the two). Did you consider using sections inside the table view instead?
Also, if you use my above solution (which I wouldn't recommend because of the UX issues), be sure to update the tableView inside the table view cell, this should best be done in the table view cell's method prepareForReuse, it could look like this:
- (void)prepareForReuse
{
[super prepareForReuse];
[self.tableView reloadData];
}
And do this only after having updated the cell in the table view controller in cellForRowAtIndexPath:
You should connect your new tableview to your CustomCell and set it as datasource and delegate, and implement that protocols in you CustomCell.
i.e. your BasicTable controls by your ViewController, but new tableview controls by CustomCell.

Resources