heightForRowAtIndexPath and customize tableviewcell height - ios

I want to know if I have customized my own tableviewcell with a specified height and this value is not the same as I used in heightForRowAtIndexPath. Which height will be used for the cell. Since I have multiple customized cells in one tableview, it will be difficult for me to distinguish each type of them in the heightForRowAtIndexPath function. I just want to know in this case, can I just use the cell height I defined for each type of cell?

You can implement this method of the UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//The height you desire to have for the row at *indexPath*
}

According to the UITableViewDelegate protocol reference,
The method allows the delegate to specify rows with varying heights. If this method is implemented, the value it returns overrides the value specified for the rowHeight property of UITableView for the given row.

My found is that before iOS8, you have to specify the row height in heightForRowAtIndexPath. Until iOS8, Apple provides autolayout and resize of cell, e.g. adding constraints.
Not sure if this is the only way to control the cell row height or not.

Related

Set height of all cells using specific identifier?

I have a table view which has prototype cells. How do I set the height of all cells using a specific identifier? For example, I have two cells; one with an idenfier of "cell10" and another with the identifier "cell50". How do I set it so all cells with the identifier "cell10" have a height of 10 while all cells with the identifier "cell50" have a height of 50? Any answers are appreciated. (By the way, I am using Swift 2.)
Within heightForRowAtIndexPath() you can call cellForRowAtIndexPath().
Once you have the cell then you can call reuseIdentifier to get its identifier.
Then return either 10 or 50 based on the identifier
I'm not sure how to do it in swift but in objective C i would do the following things-
Implement the following function of tableview-
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//your custom cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.reuseIdentifier isEqualToString:#"cell10"){
return 10;
}else{
return 50;
}
}
heightForRowAtIndexPath called first and then cellForRowAtIndexPath get called. So it may be possible that in heightForRowAtIndexPath method we do not get cell or some inconsistency.
You must have some value or field on basis of which you can specify or differentiate cell identifier. You have array of models which may contain that value.
So in heightForRowAtIndexPath(), you can get model from datasource array for that indexpath using objectAtIndex method, and check which type it is and depending on that return the height.
This will help in every case. It is working for me.

Cell content not showing up if heightForRowAtIndexPath is implemented

I'm using iOS 9.2 and XCode 7.2.
I have a basic UITableView object in which i add different kind of UITableViewCell subclasses.
With one of them, when i set manually the height overriding the method heightForRowAtIndexPath, i don't get any content on the cell.
If i return -1 as height(the default value for the UITable row height), i get my cell showing up correctly. The thing is that i do need a different height for this row because the content is quite big.
here is the code for heightForRowAtIndexPath:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MenuItem *menuItem = [menuManager menuItemAtIndex:indexPath.row];
if ([menuItem type] == MenuItemTypeEditorHeader) {
return 100;
} else {
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
MenuItem is a class containing the specific menu object' informations, such as the type. The result is that the cell is showed up at the correct height, but it's empty.
Its not advisable to use heightForRowAtIndexPath anymore - thats old-school. Instead, do this :
Set up autolayout constraints in your cell (if you dont know how to - you need to, its not something you can avoid anymore!)
Create an estimatedRowHeight for autolayout to use, on the tableView. You can set it in the nib/storyboard or programmatically, in viewDidLoad for eg, like this :
self.tableview.estimatedRowHeight = 68.0;
Set your tableview to use 'automatic dimension', like this :
self.tableview.rowHeight = UITableViewAutomaticDimension;
And thats it. If you do these things, then your cells will vary in height according to their constraints. So if one of your subclasses has a height of 150px due to its constraints, that will work perfectly next to another subclass that has a height of 50px. You can also vary the height of a cell dynamically depending on the contents of the cell, for eg when you have labels that expand using 'greater than or equal to' constraints. Also - simply omit the 'heightForRowAtIndexPath' method, you dont need to implement it at all.
Are you calling tableView.reloadData() ?
print the length of menu objects before you call tableView.reloadData().
HeightForRowAtIndexPath just returns height of a row. So may be problem in cellForRowAtIndexPath.

Table View Cell Height Dynamically + load from .xib

I m using a UITableView and several custom cells. Each cell has his own height (the cells include UICollectionView, UITableView and some more customs UIView).
More then this, the customs cells (all of them) load from .xib.
Basically, the cell height should be determine from the Model (mvc) or it is static.
In My case, I can determine the cell's height after I instantiate each one of them, e.i loadNibNamed:owner:options: or dequeueReusableCellWithIdentifier:forIndexPath: (I don't care about reuse, It's not the point here), because I need to inflate the cell's content.
As far as I know the methods calls of UITableViewDelegate and UITableViewDataSource isn't documented, therefore I don't need to consider their calls order.
How can I determine cell's height without instantiate it? And without Magic numbers.
You need to calculate row heights in following UITableViewDelegate methods based on your model
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
If your height calculation is based on network response, update your data model when response is received and call [self.tableView reloadData]

What is the different between set a UITableView cell height with setRowHeight function and tableView: heightForRowAtIndexPath: function?

Just like the title, what's the different between this two method?
I find that if I set its height with tableView: heightForRowAtIndexPath:, and then I want to get the value of height, I will get a right value by this:
CGFloat height = cell.frame.size.height;
but a wrong value by this, the result is 44:
CGFloat height = tableview.rowHeight;
But if I set it in the other way -- setRowHeight:, I can get right value in both the two way.
So what's the different between them?
rowHeight is a property on UITableView and is used for setting the table row height when the delegate does not implement tableView:heightForRowAtIndexPath: method.
From Apple docs,
You may set the row height for cells if the delegate doesn't implement the tableView:heightForRowAtIndexPath: method. If you do not explicitly set the row height, UITableView sets it to a standard value.
When row height is returned from delegate method tableView:heightForRowAtIndexPath: the value of property rowHeight is set to default (IMO, 44 points).
Apple recommends rowHeight in special use cases,
There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).
In my experience tableView:heightForRowAtIndexPath: is used when cell height is dynamic. For example, you have to display multiline text of varying height in each cell. In such cases, calculate and return the dynamic height of the cell using this delegate method.
Hope that helps!

How to increase the height of custom cell in UITableView

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.

Resources