I am coding an iPhone App in which am using a UITableView which has several sections ; each section has a header & then its rows like this:
I can do the row styles correctly, by using custom UITableViewCell objects, but how do I do the header? As you can see:
the header has the text centered with two lines drawn from it, one on each side
The header color is different
also the corners of each section are rounded
how do I achieve this, either programmatically or using Storyboard?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
This method, implemented as part of your UITableViewDelegate, is intended to allow you to to create custom views for headers in UITableViews. You can return any UIView from that method, whether it's one that you want to create in Interface Builder, or in code.
Related
I have used storyboard to design a grouped tableView with simple, static content. I want to change the look of the section headers, but storyboard does not seem to offer this functionality, and I cannot determine how to get programmatic access to the section headers without building the interface from code.
For reference, the following SO question fully resolves the problem in the case that the dataSource is dynamic.
How to change text color for Section Headers in a Grouped TableView in iPhone SDK?
Is a similar solution possible where the content is STATIC and I am NOT implementing the UITableViewDataSource protocol?
Here's an alternative approach. It will change the font size of ALL table headers and footers, which is one potential drawback, but it is simpler than the approach above I think
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class],nil] setFont:[UIFont systemFontOfSize:24.0f]];
If I remember correctly, you can click on a UIView object and drag it to the top of your table view in storyboard, and it will serve as it's header:
Otherwise, many of the tableview's delegate (not datasource) methods are still available even for static table views. So the delegate method:
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Will still be called. In here you can create a customized view to return as the header. This UIView can be created programmatically or in a .xib or storyboard view controller (via UIViewController.view).
Edit 1:
The drag and drop is for the UITableView's header, not the section. However, the method above will return a custom view for each section's header.
You can not do this via Storyboard and static cell, only table header, table footer, cells can use your custom views.
I have created an application in which i have to add users to the sqlite database.
Now the problem is I want the look of the standard iPhone Contact application Where while adding user we have the width of first cell smaller than other cells and the image before that cell..
Can you please give me the idea how such thing is possible.
How to make one cell small and rest others of normal size..
Thanks for any help in advance
There are three UITableViewDelegate messages you can listen for to adjust height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
However, even thought I didn't write Contacts.app I have a feeling they are also using
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
To adjust the views as well. Remember, you don't have to pack everything into a single monolithic custom table view cell. You can create multiple custom table view cells and load them each appropriately depending on the index path.
The contact detail view is a grouped tableview. Each cluster of cells is a section. The top section is a single custom cell with two subviews that look like squashed tableview cells. The left view shows the contact's photo. The right view shows the name.
To reproduce, create a custom UITableView subclass and lay it out like you want either programmatically or in Interface Builder. Then in the tableview delegate's cellForRowAtIndexPath check indexPath.section and return the proper row for the section.
It appears that the Contacts app uses a custom tableHeaderView when presenting the contact details with an image and label. A similar implementation is included in the sample project iPhoneCoreDataRecipes. The RecipeDetailView loads a separate nib in tableViewHeaderView that is used to set the tableView.tableHeaderView property. Have a look at RecipeDetailViewController.{h,m} and DetailHeaderView.xib. When the Contacts app switches to editing mode, the headerView appears to be swapped out for another view that has a button and a tableView with a single cell. This will allow you to set up a separate tableViewDelegate to handle the Name parts of the contact and a delegate to handle the address / telephony details.
The tableview, custom table header, and custom section headers are loaded from a NIB. The tableview is grouped.
When the view loads, the first section header doesn't show up. If I scroll down, the other section headers will appear at first, but will disappear as soon as the section above them touches the top of the screen.
If I scroll back up so that a disappeared section header is off the screen, then scroll back down, it will usually reappear.
The problem is fairly consistent but not entirely- sometimes I have to scroll up and down several times to get a header to reappear. Any ideas as to what could cause this?
I fixed it- I was using one UIView for 3 sections, changing the text appropriately then returning it. Creating separate UIViews in the NIB for each section fixed the problem. So I guess you can't do that.
Your custom views must be (or descend from) UILable or UIImageView objects. You may need to manually set row height for the headers to get custom views to load and display properly.
You should review the details in the UITableViewDelegate protocol.
Specifically, look at these methods:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
and
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
Part of the discussion on the tableView:viewForHeaderInSection: method states:
This method only works correctly when tableView:heightForHeaderInSection: is also implemented.
I would like to do a design similar to iPhone outbox.
It displays 3 lines of text in a single column. The first line is large and bold, the second line is of normal size and font and third one is grey color.
Also a time is displayed in the first line with a different font color.
I know to create a two line display using subtitle cell type but i am not sure how the outbox design is achieved.
Have they used a custom view with different labels and put that view inside the cell?
Thanks
You are on the right track. Create three custom labels and add them as subview to the cell.
You can either do that directly in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
or by subclassing UITableViewCell. Alternatively, you can design your custom cell in Interface Builder.
I am trying to create a UITableView Section that contains two buttons aligned horizontally. This would be similar to the Contacts app, where on the bottom of a specific contact page they have buttons lined up to Text the contact and share the contact. However in my tableView, I want the two buttons to be placed in between other sections - so i can't set the table's header or footer view? How would i do this?
If I understand the question correct, you want something like this
section 1
your buttons
section 2
For this you can create custom headers/footers for sections instead of table. The methods you might be looking for are in UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Look into subclassing UITableviewCell class, you can draw your custom view in there and use it, look at the UICatalog sample project in Apple site they do that there a lot