I met this issue after I upgraded xcode to 6.3. The former build compiled using xcode 6.2 doesn't have this issue and works great.
The issue is: After switch to other tab and back, or back from push segue, the width of most cells will shrink a little bit. But some don't. And it looks like following
The label content in 2nd line of cell has the correct width. And all cells display content with the correct width after first launch.
In above image, there are two uitableviewcells.
content view : blue;
background view: light green;
content Label: purple;
The bg view and contentLabel in 1st cell shrinked. That's what I don't want.
I use storyboard to set the constraints.
bg view's constraints.
labelview's constraints
I also tried to cache the cell height as per this question UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift), but it doesn't work.
//In viewDidLoad
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.contentInset = UIEdgeInsetsZero;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = [[self.estimatedRowHeightCache objectForKey:[NSString stringWithFormat:#"%ld", indexPath.row]] floatValue];
if (height) {
return height;
} else {
return UITableViewAutomaticDimension;
}
}
//cache row height. I observed this doesn't been called in first launching in debugging.
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.estimatedRowHeightCache setValue:[NSNumber numberWithFloat:cell.frame.size.height] forKey:[NSString stringWithFormat:#"%ld", indexPath.row]];
}
Thanks for your help!
Well you are not setting the Horizontal Space Constraint for the view on top of your contentView. You are only setting Horizontal Space Constraint for your label and its superview.
Do this
Select the view which contains the label
Editor > Pin > Leading Space to SuperView
Implement DataSource Method for Tableview WillDisplayCell
Or you can implement in cell class (if it's custom cell) method - (void)awakeFromNib and there set cell frame.
Related
I have to calculate dynamic height of cell depends on two label, left label and right label. left label can have empty string . I want to give a specific fixed height if left label is empty else it will take left label height. Cant able to give fixed height when label is empty
This code is not working
self.tableView.estimatedRowHeight = 50
Your constraint for your both label should be,
top,bottom,leading and trailing.
and set below code in your viewDidload
self.tableView.estimatedRowHeight = 50
self.tableView.rowHeight = UITableViewAutomaticDimension;
Set constraints from top and bottom of the cell to both labels.
Then add the following code in your view controller
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
I have a cell with a label, sometimes the label text changes and the cell should be expanded a little bit. I'm using auto layout and variable height cells (you know, UITableViewAutomaticDimension and estimatedRowHeight). It seems it is not working unless after modifying the label I call reloadData or reload that specific cell.
Is that normal? Is there any way to do this automatically or without having to use the view controller to reload the cell that now should have another height? Thanks.
I had a similar problem in one of my apps and what I did was
add 2 constraints, let d be the default height of the label
1. height of the label >= d, priority = 750
2. height = d, priority = 1000
and uitableview cell attribute to UITableViewAutomaticDimension
This worked for me.
try this one
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[[arrFeed objectAtIndex:indexPath.section] objectForKey:#"post_keywords"] isEqualToString:#""])
{
int heigthHeading = [self getHeightForText:[[arrFeed objectAtIndex:indexPath.section] objectForKey:#"post_heading"] withFont:[UIFont systemFontOfSize:17.0] andWidth:245.0];
return 25+heigthHeading;
}
else
{
int heigthHeading = [self getHeightForText:[[arrFeed objectAtIndex:indexPath.section] objectForKey:#"post_heading"] withFont:[UIFont systemFontOfSize:17.0] andWidth:245.0];
return 25+heigthHeading;
}
}
Make cell height dynamic (You can also add multiple label on same cell like this way And your cell increased height depends on labels text)
Add Four Constraints to lable (Top, Bottom, Leading, Trailling)
Set Number Of line 0 and line break mode word wrap
Add this two delegate method
-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 44;
}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewAutomaticDimension;
}
I'm attempting to dynamically resize a custom UITableViewCell to fit an attributed string's content. This is the result:
When I initialize my tableView I do the following:
self.chatsTableView.estimatedRowHeight = 72.0;
self.chatsTableView.rowHeight = UITableViewAutomaticDimension;
I also implemented the following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
As you can see it is resizing my height (the smaller allowed is 72). However, for some reason it's creating a weird effect. I'm using storyboard, and auto layout.
Move it to the - (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath method.
You have to set estimatedRowHeight to UITableViewAutomaticDimension as well.
It should work only with the two properties you set , the row height and the estimated row height.
first try to remove the delegate method.
If the table "Jumps" a little when you scroll (because of a bug) then also implement the two delegate methods, heightForRow and estimatedHeightForRow and return the same values as you set in the properties , estimatedRowHeight = 72 and rowHeight = UITableViewAutomaticDimension.
If it's still doesn't work then I would double check the constraints.
For iOS 8+
Set up cell constraints. Make sure that ALL subviews that affects cell height are vertical connected. In this case: top edge of top label is pinned to superview's top AND top labels bottom edge is pinned to bottom's label top edge AND bottom's label bottom edge is pinned to superview's bottom. Vertical constraints chain have to be closed. In your case it looks like a bottom edge of bottom label is not pinned to contentViews bottom edge.
When dequeuing cell for reuse always use
dequeueReusableCellWithIdentifier:forIndexPath: not a shorter version without indexpath specified.
Set self.tableView.rowHeight = UITableViewAutomaticDimension;
Set self.tableView.estimatedRowHeight = 50.0;. This step is optional but helps table view proper manage things like scroll indicators etc.
Will work :)
I have used the following code to dynamically change the row height in ios. But the height is too high. I dont know where i made mistake. My code is as follows:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(!self.customCell){
self.customCell = [self.goalDetailsTableview dequeueReusableCellWithIdentifier:#"GoalDetailsCell"];
}
NSMutableDictionary *cellData = [self.databaseCall transactionFromDatabase:indexPath.row goalId:self.goalId andStageId:self.stageId];
//Cell Layout
self.customCell.tipsDescription.text = [cellData objectForKey:#"tipsDescription"];
[self.customCell.tipsDescription sizeToFit];
//Height of cell
float height = (CGRectGetMaxY(self.customCell.tipsDescription.frame) + 20);
return height;
}
Note: tipsDescription is a UILabel inside the UIView which is kept inside the UITableViewCell.
You need to calculate your text height and based on it have to set cell height (yes, its going to be little mind expensive but at last you'll be happy!). Here's the one solution for this, Replacement for deprecated sizeWithFont: in iOS 7? and if you want some more info, Calculating number of lines of dynamic UILabel (iOS7) and this too UITableViewCell with UITextView height in iOS 7? (I know its not for UITextView but you can consider it with UILabel).
With iOS 8 we have self sizing table view cells. With it, there is no need to even implement heightForAtIndexPath.
Step 1) Set up constraints for your table cell
Step 2) In viewDidLoad:
self.tableView.estimatedRowHeight = 100.0; //or whatever value you need
self.tableView.rowHeight = UITableViewAutomaticDimension;
And that's it. I would also mention a few of us have ran into an issue where you might need to reload the table view's data in viewDidAppear.
I have a UITableViewCell that is implemented using storyboard that looks like:
Here is what the cell should look like without an image:
I have been fiddling with the constraints and banging my head trying to figure this out but have had no luck. I have a pretty good understanding of constraints and how to add them programmatically but have had no luck with this specific problem and feel like I am just adding layout constraints to the cell willy-nilly with no logical thought process. The cell represents a newsfeed post which may or may not have an image in the main image view at the top, and should behave as follows. If the cell doesn't have an image in it the bottom bar with the like and comment counts, moves up to align with the top of the cell. I achieved this behaviour by setting a constraint that kept the smaller image view, post title, post time and the post content a set distance away from the bottom of the cell. This approach works and when the cell is resized in the heightForRowAtIndexPath method the subviews move appropriately. The problem comes when the text in the post content is larger then a single line. The height of the cell adjusts correctly but the top of the text view stays at the same location and grows downward and overflows into the next cell. When I place the constraints to align the four subviews with the top of the cell I run into issues when there is no image and the post content is larger then a single line. In this case, the cell resizes to be smaller than its original size and the subviews stay at the distance specified by the constraint. The smaller image, post title, time and content are clipped and don't display. This is such an odd problem with so many different cases. I have been working at this for almost two days and could really use someone else's thoughts on how to solve this issue. I hope this isn't too confusing, thanks for the help!
I have one way to solve this, but I'm sure there are many others. I gave both image views a fixed height constraint. The small image view and the top label (Post Title) have fixed heights to the top of the cell -- both of these as well as the height constraint of the large image view have IBOutlets to them so they can be changed in code. The bottom label (Post Content) has its number of lines set to 0, and has an IBOutlet to its height constraint (all the labels had the standard 21 point height to start). In code, I check for the existence of an image at each indexPath, and change the constraints accordingly.
- (void)viewDidLoad {
UIImage *image1 = [UIImage imageNamed:#"House.tiff"];
[super viewDidLoad];
self.theData = #[#{#"pic":image1, #"post":#"short post"},#{#"post":#"short post"},#{#"pic":image1, #"post":#"Long long post with some extra stuff, and even some more"},#{#"post":#"Long long post with some extra stuff, and even some more"}];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.theData.count;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat ivHeight = (self.theData[indexPath.row][#"pic"])? 215 : 0; // 215 is the fixed height of the large image view
CGSize labelSize = [self.theData[indexPath.row][#"post"] sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(152, CGFLOAT_MAX)];
return 140 + ivHeight + labelSize.height; // the 140 was determined empirically to get the right spacing between the 3 labels and the bottom bar
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RDCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.label.text = self.theData[indexPath.row][#"post"];
cell.iv.image = self.theData[indexPath.row][#"pic"];
if(self.theData[indexPath.row][#"pic"] == nil){
cell.heightCon.constant = 0; // heightCon is the outlet to the large image view's height constraint
cell.ivTopCon.constant = 8; // ivTopCon is the outlet to the small image view's spacing to the top of the cell
cell.labelTopCon.constant = 8; // labelTopCon is the outlet to thetop label's spacing to the top of the cell
}else{
cell.heightCon.constant = 215; // this number and the following 2 are taken from the values in IB
cell.ivTopCon.constant = 185;
cell.labelTopCon.constant = 233;
}
CGSize labelSize = [self.theData[indexPath.row][#"post"] sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(152, CGFLOAT_MAX)];
cell.labelHeightCon.constant = labelSize.height;
return cell;
}
Hey #rdelmar thanks for the solution! Eventually I ended up just designing two different cells in the storyboard file with different reuse identifiers but the same subclass. I then checked in the cellForRowAtIndexPath method if the cell had content or not, and assigned the correct identifier. If this is the incorrect way of doing this, or will cause problems down the road please let me no in the comments.