In my custom UITableViewCell I set the height of row as
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
As a result (I believe), when data is rendered, the last cell is not rendered correctly.
This happens on all views where I set the heightForRowAtIndexPath. How do I fix it, so that I can see last cell as well?
You are returning the height of the cell correctly and this issue seems to be related with table view and not with the cell, I think you need to reduce the height of the tableView.
If you are using AutoLayout then you need to set constraints to your tableView with Respect to its superview.
Else if just set
[tableView setAutoresizingMask:UIViewAutoresizingMaskFelxibleHeight | UIViewAutoResizingMaskFlexibleWidth];
Perhaps your UITableView's height is not right, check it.
Related
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.
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.
I have a custom UITableViewCell and I'm using a prototype made in Storyboard. The prototype cell has a height of 270, and the tableview row height is also set at 270. My delegate and datasource are all set up properly, because the cells are rendering.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
SLSCase* currentCase = [_cases objectAtIndex:indexPath.row];
if(!currentCase.hasReport){
return 71;
}
return 270;
}
In the above delegate method, I'm checking from my datasource, for what size the cell should be, and the conditionals are working. (eg. if currentCase.hasReport, then the height will be full)
However, in the app, the cell is still showing as full size. The difference is that all interaction is gone, I can't tap it or anything, but I can still see the entire cell.
I've tried disabling AutoLayout as well.
I am currently using the following tableView delegate method to adjust the height of individual cells when they are selected:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 44.0f;
if (self.selectedIndexPath && indexPath.row == self.selectedIndexPath.row){
return height + 20;
}
return height;
}
This function works fine when the cell adjusted is not the last cell to be shown. However, if its the last cell, the function adversely affects the height of all the trailing empty cells as well, as shown in the illustration:
Not last cell (user "bansvsiena" selected)
Last cell ("newregistant" selected - notice all the trailing cells also have the same height)
Is there a way to fix this issue? Thanks!
There are two possible solutions for you:
Why don't you remove separator from all your empty cells? You can set yout UITableView's separator style to single line etched. Doing this will not show separators for your empty cells.
After setting self.selectedIndexPath value (I suppose you did it in tableView:didSelectRowAtIndexPath:) you are reloading your table. You can void doing this and just do following
[_tblCredits reloadRowsAtIndexPaths:#[indexPath, self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
As your all the cells will be created with 44px height at first and you won't be reloading the table so height will change only for those rows which you pass in array. This approach is much better to reload only those rows which need to reload instead of whole table.
Hope this helps :)
I have a tableview that fill with custom cells,, Now I want to increase the width of the cell,I increased the cell height of UITableview. But the problem is still same. Any one know how to increase the height of custom cell in UITableView.
Thanks,
If your cell heights vary, implement the following delegate:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0; // edit this return value to your liking
}
If they don't, merely set the rowHeight property of your table view instance to whatever height you want all of your cells to be. This answer does a good job of explaining why.