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;
Related
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'm migrating my app to iOS 7, what I have notice is that the space between Table View header and cell is small.
I think this normal behaviour f iOS 7? right.
I was look better before, Is there away to increase space in static UITableView designed using storyboared.
I think this normal behaviour f iOS 7? right.
Yes it is normal behaviour.
Is there away to increase space in static UITableView designed using
storyboard?
In StoryBoard Select tableview then changing the value under the Attributes Inspector .
For all of them which are not finding #Virus answer useful and if you have dynamic headers height for section follow this method
//This method returns the height of a header at a specific section
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//For example you define heights for these sections
switch (section)
{
case 0:
return 12.0f;
break;
case 1:
return 23.0f;
break;
case 2:
return 56.0f;
break;
case 3:
return 33.0f;
break;
default:
return 25.0f;
break;
}
}
Hope it helps
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 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 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;
}