I am trying to create a sample app to resize a cell. The cell is staying at the default height of 44px. I am unable to make this change through the storyboard because I have a view with varying height inside the cell. Can this be done with only 1 cell? I have an outlet from the table cell to the variable called cell. My code in the .m file is as follows. Also, all the cells are already static.
self.whereCell.frame = CGRectMake(0, 191, 320, 78);
Have you tried the tableView:heightForRowAtIndexPath: method?
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return 191;
}
return 44;
}
This shouldn't been needed if the cell is static though; you should be able to set the height of the cell in Interface Builder.
Related
I have a custom header in the table view cell and when i initialise it it overlaps the first cell of the tableview cell and if i increase the size of the first cell of the tableviewcell the buttons and label all go out of their positions .This is the code.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
return 100;
}
else{
return 75;
}
}
Any ideas on how to increase the height of the first cell without messing up the postion of button and labels?
I have a custom view inside a prototype cell, with a label and a UIImageView that can both be variable heights. What is the best approach to get the table cells to adjust to the view's height? It seems I have to set a height to the table cell and anything else I try won't reset the size.
i've tried the concept below with no luck.
tableView.estimatedRowHeight = 260.0
tableView.rowHeight = UITableViewAutomaticDimension
As far as I know, the height for UITableViewCell should be fixed. You could have more than 1 type of UITableViewCell inside the same UITableView. For example, the section 0 has a unique type of UITableViewCell, the UITableViewCell for the other sections have another type. Then inside the TableView Delegate method heightForRowAtIndexPath, you may set the height for the different types of UITableViewCell.
You could do something like this:-
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section==0)
return 40;
else
return 60;
}
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 want to make a UITableViewCell bigger. How can I do this, I have tried adjusting its frame but nothing happens. What can I do?
You need to implement the
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
method in your UITableViewDelegate.
You can do like this..
//Change the Height of the Cell [Default is 44]:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 1) {
return 90;
}
}
return 44;
}
I hope this will help you..
There are 2 steps to change height of a table cell without any additional code:
Select your Table View from your Storyboard. In the Size Inspector change Row Height.
Select your Table View Cell and while it's selected in the Size Inspector change the Row Height.
Note that you MUST set BOTH of Table View's Row Height attribute and Table View Cell's Row Height attribute.
If you just set one of them, it won't work.
I found this answer to be the most informative and most up-to-date to change the height of a table view cell.
TL;DR
Take extra care when making your Auto Layout constraints
Enable row height estimation on your table view in the viewDidLayoutSubviews method.
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
You can use this code for changing the height of different tableview
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == tableView1)
{
return 55;
}
else if(tableView == tableView2)
{
return 75;
}
}
This may help you.
I am trying to resize my UITableViewCell's frame via:
[cell setFrame:CGRectMake(cell.frame.origin.x,
cell.frame.origin.y,
cell.frame.size.width,
cell.frame.size.height+25)];
however, it's not resizing after I do this... why is this?
This is weird as if I add a UIToolBar into the cell, it resizes but when I am adding a UIView, it doesn't:
[cell.contentView addSubview:sideSwipeView];
Here's the long and short of it:
Your cell width is determined by the width of the tableview it's in.
[EDIT: If it's a grouped table view, the cell is 20 - 60 pixels narrower than the tableview width, depending if you're using an iPhone, or an iPad.]
Your cell height is determined by the heightForRowAtIndexPath method.
If you're manually setting the cell's frame, it's going to be useless except when you're using a subclassed cell where you want to add subviews based on the cell's dimensions.
Even in this case, it's recommended to get the cell's frame from the tableview by using rectForRowAtIndexPath:(NSIndexPath*)indexPath method and then setting that frame as the cell's frame (after setting the frame's origin Y as 0).
I'm not quite sure about the UIToolBar, but your subview's frame won't change on changing the cell frame.
Maybe if you could tell us what you're trying to achieve, we can suggest a solution for you?
--------------------EDIT--------------------
So you need to dynamically add a subview to a cell on tapping it and resize it's height according to the new subview. This is gonna get hairy so here goes:
In your .h file declare:
BOOL subviewAdded;
In your .m file, in the init, do:
subviewAdded = NO;
Let's assume that you want the cell's height to be 50 without the subview and 100 with the subview. Accordingly, your heightForRow method should be:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return (subviewAdded?100.0f:50.0f);
}
This means that initially since subviewAdded is NO, all your cells will have the smaller height.
Now, to add a subview to a cell on tapping it, and to change it's height dynamically, do this in your didSelectRow method:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the cell at this indexPath
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if(subviewAdded)
{
subviewAdded = NO;
for(int i = 0; i < [thisCell.contentView.subviews count]; i++)
{
UIView *thisSubview = [thisCell.contentView.subviews objectAtIndex:i];
[thisSubview removeFromSuperview];
}
}
else
{
UIView *someView = [[UIView alloc] initWithFrame:someFrame];
[thisCell.contentView addSubview:someView];
[someView release];
subviewAdded = YES;
}
NSMutableArray *array = [NSMutableArray array];
[array addObject:indexPath];
[tableView reloadRowsAtIndexPaths:array
withRowAnimation:UITableViewRowAnimationFade];
}
So what's going to happen here is you're adding a subview to this cell you've tapped. Reloading this cell will call heightForRowAtIndexPath and do a nice little fade animation and change your tableview heights.
IMPORTANT: Ideally, you should maintain an array of NSNumbers with boolean values. The array size should be the same size as the number of tableview cells you have.
In heightForRow, you would then check against this array instead of using a single boolean for the whole tableView. This would ensure that you could have different heights for different cells.
That would look something like:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL thisBool = (BOOL)[[booleanArray objectAtIndex:indexPath.row] boolValue];
return (thisBool?100.0f:50.0f);
}
I didn't post all that code here since it's implied and what I've posted should put you well on your way to doing the boolean array thing.
Anyway, there you are. I just tested this code myself so it works :)
If you want to increase the height of your cell based on some parameter eg. text, image,
you must implement the heightForRowAtIndexPath method of UITableViewDelegate in your code.
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath