iOS 7,cannot change height for TableHeaderView - ios

- (void)reloadUI
{
[_createNoteView layoutSubviews];
if (_list.is_archived == 0) {
_tableview.tableHeaderView = _createNoteView;
}
[_tableview reloadData];
}
I have a resizable input view called _createNoteView,[_createNoteView layoutSubviews] will correct its own frame.
each time content of _createNoteView was changed reloadUI will called.
I expect _createNoteView will will resize while I'm typing in it. But things won't work in iOS 7;
Everything works well on iOS 6 with the same context;

As your question is not very much clear, this is a general answer.
Have you used this tableview delegate method
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 60;
}
You can calculate the size of header in this method and return the specific height
The previous one is for tableview section's header.
May be this could also help
tableView.tableHeaderView = nil;
tableView.tableHeaderView = your_headerView;

Related

Height of View inside tableviewcell differs from ios 7 and ios 8

I have a uiview inside the tableviewcell. When i run the code with ios 8 the table cell look good and working fine. But when i try to run this code in ios 7 the table cell contents overlapping on the other content of the cell.
Can anyone help me to do that correctly.
Attached the Tableviewcell scrrenshot as follows:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!self.customCell){
self.customCell = [self.goalDetailsTableview dequeueReusableCellWithIdentifier:#"GoalDetailsCell"];
}
//Height of cell
float height = 360;
return height;
}
Thanks in advance.
I have solved this issue by myself.
When you are running in ios7, just set maskToBounds = YES in tableViewCell implementation file.
- (void)layoutSubviews
{
self.cardDetails.layer.masksToBounds = YES;
}

Explicitly set row height for some rows but use estimatedRowHeight for others

I have a UITableViewCell which contains a UIWebView as a subview. The web view fills the entire cell. I am using estimatedRowHeight to calculate the row height. However, at the time the table view is built, the cell has no height because the web view has not loaded it's content, therefore the cell has no content. Because of this, estimatedRowHeight returns 44 instead of the correct height of the web view content.
Does anyone know how I can correctly calculate the height of a row when the content is not immediately set? Is there a way to estimate the height of some cells and explicitly set the heigh of other cells, in the same table view?
This is how I am using estimatedRowHeight:
self.tableView.estimatedRowHeight = 200;
self.tableView.rowHeight = UITableViewAutomaticDimension;
I am not using the delegate method. I have tried it, but it does not change the result. The cell I am using has a xib which also uses Auto Layout constraints. To be clear, the cell does appear and the web view does get added to the cell. The problem is that the height of the cell is not big enough to show the entire web view. The web view loads an HTML embed code for an audio player.
I did some fiddling around and found out that you can use UITableViewAutomaticDimension in the following way:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableSection *tableSection = (self.tableModel.sections)[indexPath.section];
if (tableSection.sectionType == TableSectionTypeWebView)
{
return 120;
}
else
{
return UITableViewAutomaticDimension;
}
}
This basically says, use a height of 120 for any WebView sections but for everything else I want you to figure out the height. I am using a my own custom table model here (i.e. TableSection, sectionType, etc...)
I had to add self.tableView.estimatedRowHeight = 200; to my init method for that to work.
Now I can provide an estimated row height but also explicitly set a row height for some sections, or even some rows if I wanted.
I haven't seen any documentation for this, but I tested it with variable length strings and it held up just fine.
You make your class a UIWebViewDelegate and then in and set your class as a delegate to every single UIWebView in your UITableViewCell
-(void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
//Asks the view to calculate and return the size that best fits //its subviews.
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
Now you can get the height of the UIWebView and set it to the rows height, because the following method will be called again once a 'beginUpdates' is invoked
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath {
return _webView.frame.size.height;
}
Hope This helps

Wrong value from UITableView:rowHeight at iOS8

The code I used to create a rectangle (at least until iOS7) was
CGRect rect = [cTableView frame];
rect.origin.y += [cTableView rowHeight];
searchOverlayView = [[BecomeFirstResponderControl alloc] initWithFrame:rect];
On iOS7, cTableView (an instance of a UITableView) returned 44. Testing in iOS8 with an iPhone 5s returns -1.
Why is this happening? What is the correct code that needs to be used in order for my app to be backwards compatible with iOS7?
Apple changed the default row height in iOS8 to UITableViewAutomaticDimension, which is declared as -1. This means that your table view is set up for automatic cell height calculation.
You will either need to implement autoLayout (recommended) or implement the new delegate method: heightForRowAtIndexPath. Here's a great question about auto layout: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
Seems like you were effectively hard coding 44 (the old default) anyway, though, so you could just do that (not recommended).
This made me struggle for hours. I ended up hard coding the value to 44:
self.tableView.rowHeight = 44;
There is a performance penalty for implementing heightForRowAtIndexPath that I prefer not to incur when all rows in a table are the same height and never change at runtime (it is called once for every row, each time the table is displayed).
In this situation, I continue to set "Row Height" in the XIB and use the following iOS 8 friendly code when I need rowHeight (it works on iOS 7 and below too).
NSInteger aRowHeight = self.tableView.rowHeight;
if (-1 == aRowHeight)
{
aRowHeight = 44;
}
This allows you to keep freely editing Row Height in the XIB and will work even if Apple fixes this bug/feature in the future and a XIB set Row Height = 44 stops coming back as -1.
If you accidentally change the row height in IB from 44 to something else (like 40), automatic cell size calculation fails. You owe me 3 hours, Apple.
My solution to this problem:
#interface MCDummyTableView () <UITableViewDataSource, UITableViewDelegate>
#end
#implementation MCDummyTableView
- (instancetype) initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
frame = (CGRect){ 0, 0, 100, 100 };
self = [super initWithFrame:frame style:style];
if(!self) return self;
self.dataSource = self;
self.delegate = self;
[self registerClass:[UITableViewCell class] forCellReuseIdentifier:#"CELL"];
return self;
}
- (NSInteger) numberOfSections {
return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell*) cellForRowAtIndexPath:(NSIndexPath*)indexPath {
/*
UITableView doesn't want to generate cells until it's in the view hiearchy, this fixes that.
However, if this breaks (or you don't like it) you can always add your UITableView to a UIWindow, then destroy it
(that is likely the safer solution).
*/
return [self.dataSource tableView:self cellForRowAtIndexPath:indexPath];
}
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
return [self dequeueReusableCellWithIdentifier:#"CELL"];
}
- (CGFloat) defaultRowHeight {
return [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]].frame.size.height;
}
#end
I really don't like hardcoding things. I use this class to cache the default cell height early on in the app.
One more consideration is that if you are calculating the height based on existing view dimensions, the heightForRowAtIndexPath method may be called before viewDidLayoutSubviews.
In this case, override viewDidLayoutSubviews, and recalculate the frame.size.height value for all the visible cells.

Expanding and Collapsing header cells in UITableView

The premise is simple. I have a UITablewView. I have a storyboard where I configured two prototype cells, one has a Reuse Identifier of headerCell, the other's Reuse Identifier is cell. I am instantiating a custom header cell like this in my UITableViewController:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
MyHeaderCell* headerCell = [tableView dequeueReusableCellWithIdentifier:#"headerCell"];
headerCell.delegate = self;
return headerCell;
}
My headerCell has a button on it, and when that button is tapped, it calls [_delegate toggleHeaderSize]. You can see above that the delegate property is simply the UITableViewController. Here is the toggleHeaderSize method:
-(void)toggleHeaderSize {
[self.tableView beginUpdates];
if (headerHeight == 100) {
headerHeight = 120;
} else {
headerHeight = 100;
}
[self.tableView endUpdates];
// [self.tableView reloadData];
}
That headerHeight variable is used in the heightForHeaderInSection method, like this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return headerHeight;
}
The header changes size, but it's weird. The table slides down, but the header doesn't actually grow. If I scroll the table just one pixel, the header seems to realize it needs to redraw, and it snaps to size.
I think I've read about every SO post about resizing header cells. They all seem to think the magic is with the beginUpdates/endUpdates dance. These do seem to be necessary, but it doesn't solve the problem of redrawing.
Here's an animated GIF of my problem.
EDIT: I can't post the animated GIF since I don't have 10 reputation points, but if you go to http://nspaul.imgur.com/all/ you can find it :-)
I'd love any guidance. I can post more code if someone would like. I'll even post the whole project to Github if that helps.
-(void)toggleHeaderSize {
if (headerHeight == 100) {
headerHeight = 120;
} else {
headerHeight = 100;
}
[self.tableView reloadSections:mySections withRowAnimation:UITableViewRowAnimation];
}
Methods beginUpdates and endUpdates only create transaction for animations. You need method which will reload affected sections.

Where do you set the a UITableView's height or frame? AwakeFromNib?

I have a tableview in my app that I built in a NIB where I set the height via the Size Inspector to 68.
I also do the following in my code.
- (void) viewDidAppear:(BOOL)animated
{
self.tableView.frame = CGRectMake(0,0,320,68);
self.tableView.scrollEnabled = NO;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)awakeFromNib
{
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.tableView.rowHeight = 68;
}
When I run the app in the simulator or iPhone it looks fine, as the table is only 68px tall. If I close the app (go to the home screen) and reopen it the table is no longer only 68px tall, but it takes up the entire screen.
I can't seem to make it stay 68px all the time?
Besides my question above is there a method (awakeFromNib, ViewDidAppear, etc...) where you should always and only set the size or position of objects?
Use this method to set the height,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 68;
}
Put your code into viewWillApper, that may work!
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tableView.frame = CGRectMake(0,0,320,68);
self.tableView.scrollEnabled = NO;
[self.tableView setBackgroundColor:[UIColor clearColor]];
self.tableView.rowHeight = 68;
}
Found my own solution. The solution per Apple is that if you are using a UITableView and other subviews then don't use a uitbalviewcontroller, as the tableview will always try to use the entire screen. The solution is to use a uiview as the base with a uiviewcontroller then put a uitableview in the uiview and give the uiviewcontroller the delegate methods it needs to manage the tableview.
So just remember, if you don't want a full height uitableview then don't use a uitableviewcontroller with menubar.

Resources