Custom section header with static UITableView cells - ios

I'm wondering if it is possible to handle custom section headers when using static table cells. It doesn't seem possible through Interface Builder... the datasource is not called, so the method viewForHeaderInSection is never called too.
I'm not interested in workarounds like using a cell as section header.

Method viewForHeaderInSection is part of UITableViewDelegate protocol so you can adopt it and use that method for your section header.

Can you please check whether you have made a connection between the interface builder and your ViewController.
delegate and datasource should be connected.

Related

Why height related methods are placed in UITableViewDelegate, not in UITableViewDataSource

I found a question in a quiz is "Which UIKit protocol contains the method –tableView:heightForRowAtIndexPath:?"
without taking any load the protocol comes in my mind was UITableViewDataSource. But when I checked apple developer documentation, I found I was wrong. UITableViewDelegate is the protocol who contain that method.
Before that I understood was that these height methods are part of Datasource protocol.
Is there any specific reason of placing them in UITableViewDelegate instead of UITableViewDataSource ?
Why following function are placed in UITableViewDelegate
heightForRowAt indexPath
heightForHeaderInSection
heightForFooterInSection
and Why following function are placed in UITableViewDataSource
numberOfSectionsInTableView
numberOfRowsInSection
Before now I thought these are similar type of function so these methods must be placed in same protocol.
Please make this clear to me.
Thanks in advance
UITableViewDelegate means you provide answers to requests about the layout of the table and about actions the user performs on the tableview.
UITableViewDatasource means you provide data for the sections and rows of a table and you act on messages that change a table's data.
For more in details, Check DataSource and Delegate
numberOfSectionsInTableView and numberOfRowsInSection are functions that answer questions about the data to be displayed which is why they are to be found in the DataSource protocol.
The heightFor... functions don’t answer anything about the underlying data but only on the way the data is displayed. This is why they are part of the Delegate protocol.
Does that make sense to you?
The datasource supplies the data, the delegate supplies the behavior.
In MVC, datasource is in the model layer and the delegate is in the control layer.
Actually, on second thought, the datasource is usually controller that is lower down, closer to the model. I don't think I've ever used a model object as my datasource.
The UI prospects of the UITableView are provided by the UITableViewDelgate methods and the data (we are not talking about the data to be used inside the cell, just the information about UI (section, row, etc.) repetitions) needed to construct the UI is provided by UITableViewDataSource methods.

Migrate UITableViewController to UITableView

I currently have a UITableViewController, but would like to add a label at the bottom of the screen. I think the easiest way to do this would be to just use a UIViewController with a UITableView. Is there an easy way to migrate over? I've noticed some functionally is different; for example UITableViewController has a numberOfSections method, whereas UITableView does not have that method. Also is it possible to use a UIViewController as the delegate for a UITableView? Thanks in advance.
Update: I have attached a picture of the code, and the error given when trying to implement UITableViewDataSource.
You can do this. You need to implement the UITableViewDelegate and include the UITableViewDatasource in your View Controller, then you can use the functions you were using in your TableViewController.
Basically what you should do is add a tableview in the interface builder, or programmatically (depending on your choice). If it's done through the interface builder then outlet it to your Controller. Add the Delegate to your class, and you would have access to the tableViewFunctions (cellForRow, numberOfItems, numberOfSections, didSelectRow etc...).
Also, when you create the tableview specify the tableview.delegate = self and tableview.datasource = self.
As far as I know, the methods are the same, you just won't need the override bit before each one if its just a UITableView. You can use UIViewController as the delegate, yes, by setting myTableView.delegate = self in its viewDidLoad method.
If its just a label you need however, you might consider adding one programatically? Similar results achieved with a no data label for example - see here. You could then play around with the positioning.
This error is thrown because you need to set dataSource to your TableView Object like self.tableView.dataSource = self. After this you can use freely all methods for UITableView including numberOfSections as you mentioned.

UICollectionView: Do I need to use an UICollectionViewController?

I am using XCode5 and iOS7.
Is it possible to embed a UICollectionView into a normal UIViewController class and have the UIViewController implement the methods?
Or do I need the UICollectionViewController?
Which methods are required at minimum?
Yes it is possible to implement UICollectionView without UICollectionViewController. CollectionViews are just like tableView.
As you probably already know, when you use a UITableView you have to set a data source and a delegate in order to provide the data to display and handle events (like row selection).
Similarly, when you use a UICollectionView you have to set a data source and a delegate as well. Their roles are the following:
1. The data source (UICollectionViewDataSource) returns information about the number of items in the collection view and their views.
2. The delegate (UICollectionViewDelegate) is notified when events happen such as cells being selected, highlighted, or removed.
And new to UICollectionView, you have a third protocol you must implement – a protocol specific to the layout manager you are using for the collection view.
You can use a UICollectionView, you'll need the UICollectionView to conform to the UICollectionViewDelegate and UICollectionViewDataSource protocols.
so you will need at a minimum;
numberOfSectionsInCollectionView, numberOfItemsInSection and you'll need to implement cellForItemAtIndexPath to create the cells. Obviously you'll also need to define the Cell
Answer is NO. UICollectionView is subclass of UIVIew and can be added to any view you wish.
Since UIVIewController has view property you can add UICollectionView to it using:
[self.view addSubview:self.collectionView];
Here is a great tutorial with sample project where you can see how to implement UICollectionView in your custom UIViewController subclass:
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
You don't need an UICollectionViewController. Just make sure, that your ViewController implements the UICollectionViewDelegate and UICollectionViewDataSource.
This example should help you with the topic UICollectionView:
http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/

Why are my static prototype UITableViewCells from Storyboard showing up blank?

I defined the static prototype UITableCells, subclasses, outlets, etc. in the storyboard, but when I run the app and go to that viewcontroller/scene, the cells are there, but they have no content?
When you set up a table view with static prototype cells in Storyboards, you do so by having dragged out a UITableViewController and then subclassing it.
The trouble is that UITableViewController is oh so helpful and supplies datasource methods for you to override.
Force of habit had me getting in there and supplying default implementations for those datasource methods.
Turns out that not only do you NOT have to supply datasource methods, but if you do, they interfere with proper display of static prototype cells. I just commented out all of the datasource methods in my viewcontroller and everything started displaying fine.

How to override tableView:titleForHeaderInSection: to adjust section headers of static UITableViews?

I'm playing with storyboards and for one controller I set up a UITableView that consists of two sections. In both sections I have added a couple of static cells.
However, depending on code paths, I would like to show different headers for my two sections.
As there is no source and no delegate involved, how am I supposed to override tableView:titleForHeaderInSection: ?
With static table views you can (and must) still connect the datasource to a UITableViewController. The key is that if you implement the datasource methods then this will override the static content you have set up in the table view. You can override titleForHeader without any problems since this is what you want to do.
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
return #"HELLO!";
else {
return [super tableView:tableView titleForHeaderInSection:section];
}
}
Sets the title of section 0 in the static table to HELLO!, overriding the title set in the xib. The others are left as the were in the xib.
The key point is that static tables are populated exactly the same way as dynamic tables, except that UITableViewController implements its own versions of all the datasource methods. These methods presumably read the information from the xib file and send back the appropriate information to the table view. If you want the static content, don't implement or call super. If you want your own content, use code similar to that above.
I'm a little confused by your question, perhaps i need some more info. are you saying that in storyboard you had a view controller and dragged a table view object onto that view controller?
if thats the case then this view controller would still be able to be that table view's data source and delegate
in your .h file:
#interface VIEWCONTROLLERNAME: UIViewController < UITableViewDataSource, UITableViewDelegate>
in your .m file you would need to declare the required methods for each of those:
cellForRowAtIndexPath, and numberOfRowsInSection (refer to documentation for required and optional messages) to avoid "incomplete implementation error". this is also where you could override titleForHeaderInSection.
and back in storyboard you would right-click on the table view and drag the data-source and delegate option to your view controller (or do so in the Connections Inspector tab)
if this is not what your doing please provide some more info.

Resources