Custom UITableView section header, weird behavior - ios

I'm using a custom header view for the section headers in a UITableView. Here are a couple of dataSource methods:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SBTableViewHeader *header = [[SBTableViewHeader alloc] init];
header.titleLabel.text = self.earnings;
header.subtitleLabel.text = #"You have earned $23 today.";
return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 90;
}
I know that my header view is supposed to be 90 points high, but instead I'm getting 90 points of my header view PLUS another 90 empty points of space, which cover some of the cells. That's 180 total, measured it in Photoshop.
Help!
Some more info: SBTableViewHeader is a UIView subclass and is using a .xib. Nothing special there. File's Owner is set to SBTableViewHeader and everything else seems to be connected properly.

(From the comments:)
You have to use initWithFrame instead of init to create a view with the correct size.
heightForHeaderInSection: (and similarly heightForRowAtIndexPath) is used by the table view only to compute header and cell positions, the size of the scroll indicator etc.
The data source (or delegate) methods have to return cells/views of the correct size, otherwise they will overlap with other items.

Related

Uneven section header size in static UITableView

I've created a simple static UITableView which is embedded in a UINavigationController. I don't understand why the height of the first section header is greater than that of the height of the section header after the first section. I want the height of the section headers to be the same.
Note: I'd like to do everything in the Storyboard if possible.
I don't think you can do this just in Storyboard. The only way I have been able to get things looking the way I want is to implement something like;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section==0)
{
return 25;
}
else
if (section==1)
{
return 35;
} // etc etc
And then, in Storyboard, adjust the Table View Size, Section Height values.

UITableView section header size can't vary size

Hi I am trying to size the headers based on the views. However,it only resizes the first header. The others are just set to the default. I reset the size in the viewForHeaderInSection function.Here is the code for that:
sectionHeight[section]=descriptionView.frame.origin.y+descriptionView.frame.size.height+5;
[self tableView:tableView heightForHeaderInSection:section];
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return sectionHeight[section];
}
sectionHeight is a pointer to an array of floats that contains the heights and were initalized as 50.0 in viewdidload. After checking the variables, I confirmed that the values did change, however the section header still had a height of 50 pixels.

Remove space at the top of first row in table view

In my application i'm using custom cell in table view to display images in all rows. But in my .xib file there is a big empty space comes at the top of the custom cell. Can anyone tell me that how remove that space in iOS7 (iPad)? (like how to change the Y axis of the custom cell)
This is an IOS7 related issue with UITableViewStyleGrouped and size of footer views in section.
If you set the footerView to be size 0 it defaults to a larger value, The solution I found was to set the footer to a very small non zero number
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.0000001f;
}
Try to set into your property inspector like this..
This might help you:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.hidden = YES;
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
Sometimes the empty space in the first row is the section header space. This functions eliminate it.

How to add footer view for UITableView in IB when working with storyboards?

Playing around with prototype cells and I need to add footer view at the bottom of table view, so a user could see this footer view when he would scroll to the bottom of the table view. So, created demo project with one screen, table view and two prototype cells. Looking for a way how to drag and drop some view below the table using Interface Builder. The problem is it looks like view is put outside table view content, so I see the footer but I can't scroll to it (only a small part of footer view is seen at the bottom of the table view).
I know this should work because already saw a working implementation but cannot figure out what magic setting or code line I need to add.
Here are the methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat rowHeight = 10;
switch (indexPath.row) {
case 0: {
rowHeight = 376;
break;
}
case 1: {
rowHeight = 105;
break;
}
}
return rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0: {
Cell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:#"Cell1" forIndexPath:indexPath];
return cell1;
break;
}
case 1: {
Cell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:#"Cell2" forIndexPath:indexPath];
return cell2;
break;
}
}
return nil;
}
Just making 2 row table with two different height cells. Cell1 and Cell2 classes are empty subclasses of UITableViewCell.
Here's how table view and cells look in interface builder:
Here's initial view after launch:
Here's what I see if I scroll to the bottom:
The footer is there, but outside table view (scroll content). As you can see, table view by default reserves some 44px space at the bottom for footer. But if I set footer height in tableView:heightForFooterInSection: then blank spaces appear.
Also, tried to drag and move this view up to view hierarchy in IB, so the view would become a header view. In that case, the view is shown at the top as header view, but then the second cell is shown only partially when scrolling to the bottom. It looks like table calculates how much space it needs to show prototype dynamic cells (have set "Dynamic Prototypes" for the table view). And if you add extra footer or header view to the interface builder then there's less space for the cells (if view is added as header) or the footer is not shown.
UPDATE. If I add this method then blank spaces appear:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 50;
}
Here's what I get in that case (blank spaces below cell2):
UPDATE2 Footer is shown correctly if I disable autolayout.
I think you have missed the following line
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return HEIGHT_OF_YOUR_FOOTER_VIEW;
}
Edit
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footer=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,50.0)];
footer.layer.backgroundColor = [UIColor orangeColor].CGColor;
return footer;
}
Updated
Check this documentation
At last found the solution. Have noticed footer is shown correctly when autolayout is disabled, so started to look at constraints. So, have added leading space to container, trailing space, bottom space and top space to container constraints to table view using Ctrl + Drag. However IB showed red warning about missing Y position constraint. So, after choosing "Add missing constraints" from IB suggestion panel, IB added another system constraint but the footer was still not show correctly. It appears IB was unable to add correct top space and bottom space to container system constraints, and even to fix that. So I'v got 5 system constraints as a result of that:
Adding system constraints using Ctrl + Drag from table view to containing view have worked in previous demos for me. However, this time IB was unable to add correct top space and bottom space to container, and so top space vertical constraint had a value of -568. Tried setting to 0 but it didn't worked. Tried ten times to delete all constraints and add them again. The same result.
So, I deleted all these vertical (bottom and space) constraints and then selected "Add missing constraints". And bingo! IB added correct vertical constraints and the footer view was shown correctly. Here's how correct constraints should look like. However, I still don't understand why IB was unable to add correct constraints when I was doing Ctrl + Drag from table view to container view.

Dynamically size cell based on content iOS

I have a custom cell shown in attachment:
I am using a UIWebView to display tweet text. Some of the tweets are short and I want to resize the web view, move up the Time Since, and then finally resize the cell. I know how to resize the web view, but don't know how to figure out how to move the time since and resize the cell.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSFetchedResultsController *fetchedResultsController = nil;
if (self.tweetsSegmentedControl.selectedSegmentIndex == REPORTERS_SELECTED_INDEX)
{
fetchedResultsController = self.reportersFetchedResultsController;
}
else if (self.tweetsSegmentedControl.selectedSegmentIndex == PLAYERS_SELECTED_INDEX)
{
fetchedResultsController = self.playersFetchedResultsController;
}
Tweet *tweet = [fetchedResultsController objectAtIndexPath:indexPath];
//Calcaulate height of box, instead of return a fixed number
return 170;
}
I know how to resize the web view
Don't. Use autosizing to stretch the UIWebView with height of the cell.
how to move the time since
Don't. Use autosizing to pin Time Since to the bottom of the cell.
resize the cell
Use -tableView:heightForRowAtIndexPath:
Update
The way I did this before is to set a known size for the flexible view (for you the web view) in the prototype cell and set a known size for the prototype cell. In this case we'll say the web view is kPrototypeViewHeight and the prototype is kPrototypeCellHeight.
In the table view controller:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIWebView *webView = … // Whatever you need to do to get the web view
CGFloat webViewHeight = webView.frame.size.height;
return kPrototypeCellHeight - kPrototypeViewHeight + webViewHeight;
}
Note: This will be done before -tableView:cellForRowAtIndexPath: is called. So you need to know your size before you have to draw yourself.
Note 2: Because you are changing the size of webView yourself, you will not want it autosized.
Note 3: You will be redering each web view in your table to calculate your height. My guess is scrolling will be very slow.
Hope that helps.

Resources