I am using variable heights for my UITableViewCells. This works fine normally however
I am finding that whatever the very last cell's height is, that becomes the height for all of the blank cells below it.
Is there a way to set a default height for the blank cells that appear below the last cell?
Thanks.
if you are using switch statement then set the default height for empty cells in default case of switch. Like so
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int rowHeight = 40;
switch (indexPath.row) {
case 0:
rowHeight = 80;
break;
case 1:
rowHeight = 300;
break;
default:
rowHeight = <your default value>;
break;
}
return rowHeight;
}
Related
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;
}
The 2 rows on top (Monthly, Annual) are a different section than the 3rd row at the bottom (Lifetime). I want to remove the space that is above the Lifetime row in between the sections. I tried changing the header height to 1.0 in the heightForHeaderInSection method but it didn't seem to work.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
CGFloat headerHeight = 0.0;
switch (section) {
case 0:
headerHeight = [self featuredProductHeaderView].frame.size.height;
break;
case 1: {
if (_groupOneSectionTitle) {
headerHeight = 1.0;
}
}
You need to set the footer height as well, and make sure you are not returning a title for header/footer nor are you returning a view for header/footer.
I've created cell with 4 different imageViews where i've created dynamic constraints, so they will change the width and height equal to the device size, but since cell is not dynamic then it won't change the height of the imageView. Therefore I want to set the cell Height equal to the image Width, so the images always will have the same width and height.
In heightForRowAtIndexPath, I've tried to return following, but this is giving me an error: (lldb)
let cell = tableView.dequeueReusableCellWithIdentifier("ImageViewCell", forIndexPath: indexPath) as ImageViewCell
return cell.image1.frame.width
How can I do this?
you cant call cell object within heightForRowAtIndexPath because it creates infinite loop as cellForRowAtIndexPath calls heightForRowAtIndexPath. secondly, adjust width using constraints
as function name clearly indicates that it is for height.
If you are sure about the height of the control, then use this
return 90.0 //its optional if height is fixed
else
add constraints for top, bottom,left ,right, width and height constraint, and use >= condition for height constraint.
I think you need to use to Tableview's Delegate to set its cell height. maybe you can try somethin like that:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.row) {
case 0:
return image.frame.size.width;
break;
case 1:
return image.frame.size.width;
break;
default:
return 44;
break;
}
}
I think you need to get the size of UIimage from your datasource. not from your cell.
I understand that using a dynamic TableView, you can set the Cell Height using...
tableView:heightForRowAtIndexPath:
However, if I am using A Static Table View ( not Dynamic), With Sections, I would like to programmatically set the height of a cell to 0.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//used to set the height of a cell
CGFloat result;
switch(indexPath.section) {
case 0:
{
//How do I get the ROW number in this section?
result = 44;
break;
}
case 1:
{
//this works if I want to set all of the rowels to this height.
result = 250;
break;
}
}
The question asked another way, If I have the indexPath.section... how can I do something like...
indexPath.Section.row?
You can use indexpath.row directly. No need to write indexPath.Section.row.
In iOS, the NSIndexPath gets extra duties. It is a bit confusing as Apple has two different pieces of documentation.
As noted in the other answers, you get:
Getting the Section Index
section property
Getting the Index of a Row or Item
row property
item property
I am trying to create a grouped table view with 3 groups, and each group has cells that are a different height. I've set up 3 prototype cells in my Storyboard with the appropriate heights, and have set up my UITableViewController to return the appropriate heights for each row. However for some reason all of my cells are only 1 pixel tall when they appear on screen.
I've stepped through the code and the correct height and cell is returned for each row. I've also verified that the frame of each cell is correct before it is returned. However calling rectForRowAtIndexPath reveals that the rect for each row is only 1 pixel tall.
This problem goes away entirely when using a constant cell height. In that situation the cells display perfectly, some of them are just too short/tall since they should be variable height.
Any ideas what is going on here? I'd like to undertand how the rect for each row is determined when using heightForRowAtIndexPath.
Code for heightForRowAtIndexPath:
- (NSInteger)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
return 80.0;
break;
case 1:
return 260.0;
break;
case 2:
return 110.0;
break;
case 3:
return 114.0;
break;
default:
break;
}
return 0;
}
You have declared tableView:heightForRowAtIndexPath: to return NSInteger.
That should be CGFloat.
simply to increase the height for row .simply write the method for height for row at indexpath an simply written
if (indexpath.section== 0)
return 50;