Populate data to UIcollectionview cell created with xib - ios

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.

Related

Forward call to delegate at runtime

I have several subclasses of UITableViewController which contain cells of type myCell (a subclass of UITableViewCell). These cells contain UITextFields and are the delegates for these textfields. However, in rare cases, I would like to catch some of the calls to the delegates in the tableViewController (in particular, textFieldShouldBeginEditing:).
Is there a reasonable way to achieve this without having to subclass my cell ?
Have your myCell class define its own protocol as well as a delegate property.
Then make the table view controller the delegate for each cell it creates. Then the table view controller can implement the cell's protocol methods.
Then the implementation of the cell class can call, when needed, the methods of its delegate.

How UITableView working without registerClass:forCellReuseIdentifier:

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

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.

How to use a TableView inside a UIViewController?

I want to add a simple static TableView to my UIView. So i dragged it into my UIView. Added the TableViewDataSource and Delegate Protocols to my ViewController class. Created an outlet to my tableview and connected the datasource and delegate outlets to the viewcontroller.
But i still get an error message which says that Static table views are only valid when embedded in UITableViewCOntroller Instances ? Any Ideas how to solve this Problem ?
PS: I am using UIStoryboards for designing the UI
Use a containerView (drag one onto your UIView) which will link to a UITableViewController instead. You will use the prepareForSegue method to initialize the table view controller.
Oh, and you might want to accept answers that help you or no one will help you anymore.
If you want static cells, in your Storyboard, click on the tableview, then in the inspector, change the Content from 'Dynamic Prototypes' to Static Cells. You can also segue these cells to a new controller without having to set up data sources and delegates for the table.

Setting the subclass of a UITableViewController

I have created a project where I have a UITableViewController with static cells. I have created a UITableViewController subclass so that I can write the code for some of the controls I want to put in the static cells - however when I set the custom class of the UITableViewController to that of the subclass I've created, the table isn't displayed. Coudl anyone please help me with what to do that the table will be displayed after I've set the custom class of the UITableViewController?
Thanks
Assuming you are using storyboard and static cells: make sure the datasource methods in your controller do are not overriding the number of sections and rows in the table view. With static cells you can delete both numberOfRowsInSection: and numberOfSections.

Resources