There's more content in my Table View, but this cell is getting cut off and not showing more content:
... So I'm not able to scroll any more, even there is more content.
If I pull up with my finger it shows more content, but then when I let my finger off the cell it goes back to the state show in the image above.
I've tried making sure I set height and width in the Labels and Images in AutoLayout since I thought that might be a problem, but still hasn't fixed it.
Any ideas? Thanks!
UPDATE - Table View structure in Storyboard
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
id model = self.model[indexPath.row];
if ([model isKindOfClass:[DBase self]]) {
return 520;
}
else {
return tableView.rowHeight; // return the default height
}
}
I was having this problem as well but mine was only cutting off a single cell. I solved it and thought I'd post this here as it may help others in the future
I was using custom cells created from nibs. Some of my cells could change heights so I was also using this:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 61
I figured out the issue was that I was setting top, left, right and height constraints on my view. I needed to set the bottom constraint as well or else my row height would be way smaller than it should be.
So if you are having this problem check if your row heights aren't smaller than the views that they contain.
I suggest adding height and width constraints to your UITableViewContoller or UITableView in the storyboard.main.
Depending on the size of your device, the UITableView size will remain constant unless you set constraints that will work across all devices.
Related
I am using table view for my chat application, in which the chat responses are added either one of two textviews as shown in the image. The thing is whenever I add a large text, the cell height does not expand to show the full text. It only shows the last line of that text. I am using objective-c. Is there a way to solve this ?. I do not want to completely create a new view like the questions asked before, I want changes to this specific view.
First, you need to use Label and set top and bottom constraint between label and contentView cell.
In order to allow the self-sizing cell mechanism to work, you must set the rowHeight property on the table view to the constant UITableViewAutomaticDimension. Then, you simply need to enable row height estimation by setting the table view's estimatedRowHeight property to a nonzero value, for example:
_tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
_tableView.rowHeight = UITableViewAutomaticDimension;
You need to defined self-sizing cell like this.
tableCountries.estimatedRowHeight = 100.0;
tableCountries.rowHeight = UITableViewAutomaticDimension;
Make sure you have a proper constraint. You can have a constraint like this.
In the below label you need to set your textView top position. That's why when the below label expands it automatically expand label and text view will push to bottom.
I think your cell is not gettings its height because of textview scrolling maybe that will help:-
Use UITableViewAutomaticDimension and for that your tableview cell content should justify both top-bottom leading and trailing constraints to calculate cell height.
Set scrollEnabled to "false" for your textview.
With everything on the place your cell will get its height for sure.
you can do it in many ways :
1) You can set dynamic height of cell using UITableViewAutomaticDimension.
You need to set proper constraints in storyboard like setting top and bottom.
in ViewDidLoad add below :
tblView.rowHeight = UITableViewAutomaticDimension
tblView.estimatedRowHeight = 100
and in tableView cell's height method :
return UITableViewAutomaticDimension
2) Find text's height dynamically using below function :
let width = 100.0 \\your text label's width you gave
var height = chatMsg.text?.height(withConstrainedWidth: width, font: chatMsg.font!)
using above code you can find the height of your text. and you can use it dynamically
3) there are many code available to use for chat application in which you just need to pass text/image, and it will handle everything else.
Link : https://github.com/kerry/iMessageBubble
Add these two delegate methods:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
Create Custom UITableViewCell with Three UILabel like lbl1,lbl2,lbl3.
Add the AutoLayout Constraints to lbl1 left, right, top and bottom. With fixed height (30).
Then set same constraints to remaining two Labels.
Here is main things:
1.select lbl1 heigh and double click on height constraint and set >=30
Do same things with lbl2 and lbl3.
Final step is:
Select lbl2 top constraints, change the constraints property like <=8.
Do same things with lbl3, that's it we are done all.
thanks..
I have a storyboard with several different types of prototype cells with UILabels containing dynamic data. In my storyboard, the cell looks like this:
The UILabel's Lines property is set to zero to allow multiple lines of text. It is pinned to the top, left, right of the content view and to the nearest neighbor on the bottom (the blue line). The blue line is pinned to the left and right of the content view and to the UILabel at the top, and the UITextView at the bottom.The UITextView is pinned to the content view on the bottom, left and right, and to the blue line at the top.
When I run the app I get the following:
So the UILabel is forcing everything else down, as it should be, but the cell's height does not change as I want it to and thus the text view is being clipped off by the cell's fixed height. It was my assumption that if everything were pinned at the top and bottom, then the content view would be forced to expand. What am I missing here? Thanks!
It seems you have set your constraints correctly, just make sure that delegates are as below:
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
(If your constraints are set correctly from top to bottom)And that's it, you dont have to do anything else, auto layout will do its work smartly.
In addition to Auto-Layout constraint
try cell.contentView.LayoutIfNeeded statement in cellForRowIndexPath just before returning cell and HeightForRowAtIndexPath method
If you're sure all your vertical constraints are set and correct top to bottom, try setting tableView.rowHeight = UITableViewAutomaticDimension explicitly.
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'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/
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.