UITableView section header size can't vary size - ios

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.

Related

How to increase UITableView Height Dynamically When number of cell is increased using Objective C?

I am new to iOS. I have implemented a table view which is populated with array values successfully, but I'm not satisfied with the result. When I scroll my table view the top values are hiding.
Here is my problem: based on the cell height the table view height should also get increased dynamically in UITableview. How can I accomplish that?
For UITableViewCell dynamic height you can use following delegate by return UITableViewAutomaticDimension
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 300; //give maximum heigh you want
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
and to increase tableview height based on content size use the below line of code
YOUR_TABLE_VIEW_HEIGHT_CONSTRAINT.constant = [YOUR_TABLE_VIEW contentSize].height;
Hope this will help you

UITableView Rows Height After Loading Cells

I have UITableView that contains 4 different types of customized cells in storyboard. Each cell has customized UILabels which get variable amount of text data from backend. I am struggling with making the cells resizing correctly. I would really want to change the height of each cell but I can not use heightForRowAtIndexPath because it is called before cellForRowAtIndexPath, but the height is actually calculated within each customized cell.
I tried writing in each cells' height into an array while the UITableView is loading, then just reloading it all over again once, but no effect. I tried using CGFloat rowHeight = UITableViewAutomaticDimension with no success either. The customized labels in each cell definitely grow with text which I see when I just statically change row height to higher numbers. So, I would need somehow my labels to push on rows to make them grow, not sure.
Different similar posts on stackoverflow that I found did not help.
The issue was that I needed to set up top and bottom constraints to the ContentView and NOT to the cell itself in the storyboard.
Label -> ContentView top and bottom constraints need to be set up. And then UITableViewAutomaticDimension specified in viewDidLoad:
self.tableView.rowHeight = UITableViewAutomaticDimension;
estimatedRowHeight should be set too. For example:
self.tableView.estimatedRowHeight = 76.0f;
First Method called is:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
Second:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Then:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
set a break point in the above methods and test it. So if you want to preset the height use estimatedHeightForRowAtIndexPathmethod.

How to get iOS tableview last right content size with use auto layout

I have a tableview UITableViewAutomaticDimension for auto layout, and I get the tableview content size to make the tableview height as it content height after tableview cell configure and reload, this height isn't right it seems calculate by estimatedRowHeight. I know I can get the last right height use kvo to observe the tableview content size, but I don't know which is the last right one, because I just want do something once when content size is right. How can I do that?
you need to add a delegate method like
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
before that you should pin 4 sides on that contents which you dropped in prototype cell.
now you got the exact row heights of each cells.

iOS8 automatic tableviewheight wrong on first cells

I'm using Autolayout on iOS 8 and make use of UITableViewRowAnimationAutomatic and all of its magic.
When the Tableview appears the cells have a wrong height and the subviews are distributed over the whole cell with a lot of horizontal space between them. When I scroll down or rotate the device and back alls cells are drawn correctly with the correct size.
Debugger says no error and Autolayout warnings are not present.
These cells are only drawn on iPad and I have specified only for sizeclass (Regular | Regular).
Do you any hints what could be the problem?
There are three things that you need to make sure you are doing...
The AutoLayout Constraints should cover the entire height of the cell. So just by looking at the constraints you should be able to say exactly how tall the cell is.
Implement the estimated height for row method...
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return an actual number here. This is a guess of how tall the cells are
return 100;
}
or
// Thanks #rdelmar :-)
self.tableView.estimatedRowHeight = 100;
Implements height for row...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return auto dimension here
return UITableViewAutomaticDimension;
}
Once you have done all three of these it will work.
See my blog here for more data (note, there has been an update since I wrote the blog which I haven't updated yet).
http://www.oliverfoggin.com/using-a-static-uitableview-as-a-layout-device/

Custom UITableView section header, weird behavior

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.

Resources