After fetch data,i reload tableview. I use automatic dimension
don't override heightForRowAtIndexPath like below
self.tableView.estimatedRowHeight = 200;
self.tableView.rowHeight = UITableViewAutomaticDimension;
Everything is ok in iOS9. But in iOS8 appear like following link until scroll.
iOS8 appear
I tried:
[_tableView setNeedsLayout]; [_tableView layoutIfNeeded]; [_tableView reloadData]; After that occur else ui issue in iPhone 6Plus.
I set preferred Width automatic and not marker explicit.
I set uitableviewcell accessory type none.
Tried reload section.
Tried after did display cell, reload table view. After that while scrolling another view controller in page controller, occur same issue.
Tried [cell layoutIfNeeded]
Tried setPreferredMaxLayoutWidth
Try to reload data in viewDidappear or viewWillappear and check your constraints that it is perfect or not because UITableViewAutomaticDimension strongly recommended autolayout.
Update :
According to comments try to add this two line in cellforrowAtindexpath
[cell layoutIfNeeded];
[cell updateConstraintsIfNeeded];
In order to make UITableViewAutomaticDimension work you have to set all left, right, bottom, and top constraints relative to cell container view. If you missing like this then check that
hope this
will help :)
Related
I have a UICollectionView that I set the translatesAutoresizingMaskIntoConstraints to NO, and added some constraints. When I try scrolling it to an indexPath:
[self.datesCollectionView selectItemAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
Then it completely ignores that, and doesn't scroll. But when I remove translatesAutoresizingMaskIntoConstraints, then it scrolls, but the constraints are ignored.
My question is, How can I get the collectionView to scroll to an indexPath when translatesAutoresizingMaskIntoConstraints is set to NO?
After looking at your code, I found the problem. I'm not sure why this is the case, but the contentSize of the collection view was zero after applying the constraints. Adding a call to layoutIfNeeded to either setSelectedDate: or setDates: fixed the problem,
- (void)setDates:(NSArray *)dates {
_dates = dates;
[self.datesCollectionView layoutIfNeeded];
[self.datesCollectionView reloadData];
self.selectedDate = nil;
}
EDIT
Built a new sample project from scratch and the dynamic tableview cell height is working flawlessly. I then tried to replicate by trimming down my project to it's bare minimum and it's STILL broken (first few cells buggy). Attaching full project for both working and not working examples. Project/code literally looks 99% identical and for the love of me can't figure out where the 1% difference is. The only thing that is popping out at me is that I used different size classes (wAny hAny vs wCompact hRegular but I can't imagine that would do anything given I'm testing in portrait only
Working project:
Not working project:
WORKING (new project from scratch):
https://drive.google.com/file/d/0B_EIkjmOj3ImWXZjVFZMYXZmVGc/view?usp=sharing
NOT WORKING (my project, cleaned up to it's bare minimum)
https://drive.google.com/file/d/0B_EIkjmOj3ImMGRNOXU2RlNkWEk/view?usp=sharing
Have scoured the web trying to understand what is going on, but for some reason my cell heights are incorrect until I scroll past the prototype cells.
Upon initial load:
And after scrolling past each cell:
Background colors:
cell.postTextLabel.backgroundColor = [UIColor orangeColor];
subView.backgroundColor = [UIColor blueColor];
contentView.backgroundColor = [UIColor brownColor];
Not doing anything too fancy here: just a prototype cell, a subview, and three labels (username, timestamp, text).
Below screenshots highlight my constraints in Storyboard:
I pull data from Parse, and am reloading my data while setting tableView layout
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
[self.tableView reloadData];
And lastly my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self postCellAtIndexPath:indexPath];
}
- (UITableViewCell *)postCellAtIndexPath:(NSIndexPath *)indexPath {
static NSString *PostCellIdentifier = #"PostCell";
PostCell *cell = (PostCell *)[self.tableView dequeueReusableCellWithIdentifier:PostCellIdentifier forIndexPath:indexPath];
[self configurePostCell:cell atIndexPath:indexPath];
[cell setNeedsLayout];
[cell layoutIfNeeded];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}
Finally i found the problem why it is not working. I downloaded the Projects which were uploaded to drive by you, and do modifications in it. And found the issue that you use compact Regular size classes which causes error. After converting your size classes to Any, Any it works fine and perfect.so the key of the solution is change wCompact hRegular to wAny hAny. There is no change in code.
That was initial answer.
Detailed Answer :
1) for the size classes first read this document of apple.
2) In your broken demo the size classes are selected as given below :
This indicates that you selected specific view like compact width and regular height means this auto layout will only work in specific iPhone portrait orientation.
Compact Width | Regular Height combination specifies layout changes
that apply only to sizes resembling iPhone devices in portrait
orientation.
The control wCompact hRegular indicates the compact width and regular
height size classes.
I used below size classes and also in your working project i can be able to see below size classes :
In this Size classes it's working fine.
I don't know is it apple's bug or what. !!!
If you want to support only portrait mode and only iPhones then you can choose iPhone in Development Info -> devices. and for orientation you can choose Portrait and upside down in Device orientation.
Hope this helps you to figure out the problem.
I have met the same issue, just try to use a larger estimatedRowHeight which can contain your content. SDK seems do something wrong when using self-sizing cell.
Just set your row height programatically.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
{
return rowHieght;
}
for my issue, I set the UILabel font in
layoutSubviews:
thats causing the problem.
after I moved the code to other place, then it works just fine
I also had the same issue,
how-to-add-uiview-in-uitableviewcell
You also have custom cell, so use the following function in customcell class, in your case it is PostCell.m.
- (void)layoutSubviews
{
[super layoutSubviews];
// in my case i only had set frame here while my other
// declarations were in init function, so it solve my problem
}
It solved my same problem, hope it will solve your too, but you have to think it in your own scenario.
First of all you have to set your Detail label line 0 then give leading, trailing and bottom constraints.
in viewDidLoad method add:
tableview.estimatedRowHeight = 50.0 //(your estimated row height)
tableview.rowHeight = UITableViewAutomaticDimension
then return the row height in tableview delegate get method cell row height
In Your code
[cell layoutIfNeeded]
[cell updateContraintsIfNeeded]
are not needed so remove it
I have a custom UITableViewCell with objects in it (such as UILabel, UITextView, UITextField etc.). When a button gets selected, a cell gets added to the tableView.
When I run it on the simulator, and the cell gets added, all the visible cell's and subviews height get really compact. (I do have auto constraint applied.)
....
[[self myTableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
If I do the following, the cells get back to normal:
NSArray* visibleCellIndex = self.myTableView.indexPathsForVisibleItems;
[self.myTableView reloadRowsAtIndexPaths:visibleCellIndex withRowAnimation:UITableViewRowAnimationFade];
[self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
The problem with reloading the visible cells, is: First, that's a workaround, not getting to the source of the problem. Second, it's not a fully functioning workaround, because the whole tableView scrolls all the way up for a second, then scroll back to position.
The reason why it was shrinking, is because, you have to implement the method of heightForRowAtIndexPath.
The only problem now, is that the tableView jumps up, then scrolls to position. Don't know why.
Does your target run only on iOS 8 and later? If yes, you can set self.tableView.rowHeight = UITableViewAutomaticDimension to enable Autolayout for your cells. Then, you also don't need to implement delegate tableView:heightForRowAtIndexPath:.
If you're already doing this, your problem probably lies in your custom cell. Maybe its constraints are not well defined? How do you initialize the cell's constraints?
Another idea is to trigger the layout pass manually in tableView:cellForRowAtIndexPath:. After the cell has been initialized and its text label values have been set, call:
[cell setNeedsLayout];
[cell layoutIfNeeded];
I created a dynamic cell using AutoLayout, put code in (void)updateConstraints cell's subview method, set BOOL value so custom AutoLayout code runs only once, and in View Controller call
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
just before returning cell. Everything seems to works fine but I'm experiencing weird issue, when I select cell all its subviews (or itself?) change position.
cell pointed with this beautiful arrow shows it ;]
I experienced the same bug and tinkered for about a day to fix this ... to get to the bottom of the issue I set different background colors on the cell & the content view. Right after first showing the table view everything seemed to work, but after selecting the cell the contentView would jump around - sometimes when selecting, sometimes when deselecting (which I do immediately in tableView:didSelectRowAtIndexPath:), and the cell's background became visible. So I figured the cell somehow didn't reestablish the correct contentView dimensions.
Finally it turned out that at least in iOS 8 you need to set the appropriate autoresizingMasks for the cell as well as the contentView, and then make the layout system translate them into constraints by setting translatesAutoresizingMaskIntoConstraints to YES. At the same time this flag needs to be NO on all subviews of your contentView.
So here's my initialisation for custom cells:
- (void)awakeFromNib {
// enable auto-resizing contentView
[self setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.contentView setTranslatesAutoresizingMaskIntoConstraints:YES];
self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// remove autoresizing constraints from subviews
for (UIView *view in [self.contentView subviews]) {
view.translatesAutoresizingMaskIntoConstraints = NO;
}
}
Works like a charm for me. For the record, I'm using the UITableViewCell auto-sizing which was introduced in iOS 8, like this:
// enable automatic row heights in your UITableViewController subclass
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80.0; // set to whatever your "average" cell height is
I found solution to this issue,I used instructions under this link dynamic-cell-layouts-variable-row-heights to implement dynamic table view cells: [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
[1]: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights. Author suggests to use this two methods to update constrains when using updateConstraints method inside cell's subview.
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
but what I found in my case is there was some issue that not all cells was updated as they should. Solution is to call [cell updateConstraints] in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and not wait until system will update constrains.
I have setup a tableview to use self-sizing cells (in fact I didn't have to do anything more than use auto layout - it seems that UITableViewAutomaticDimension is the default rowHeight).
The cells are sized correctly:
However, when I push another controller on to the stack, the cells immediately resize just before the transition animation:
They remain at the incorrect size when I pop back to the table view controller. I can partially resolve this part by reloading on viewWillAppear to recalculate the correct heights, but then I lose some other animations and selection state.
Is this an iOS bug? How might I work around it?
Here is a sample project that demonstrates the same.
You need to set estimatedRowHeight:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[Cell class] forCellReuseIdentifier:#"Cell"];
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80;
}