Retrieve Custom Prototype Cell Height from Storyboard

Retrieve custom prototype cell height from storyboard?

For static (non-data-driven) height, you can just dequeue the cell once and store the height:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSNumber *height;
if (!height) {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
height = @(cell.bounds.size.height);
}
return [height floatValue];
}

For dynamic (data-driven) height, you can store a prototype cell in the view controller and add a method to the cell's class that calculates the height, taking into account the default content of the prototype instance, such as subview placement, fonts, etc.:

- (MyCustomCell *)prototypeCell
{
if (!_prototypeCell) {
_prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
}
return _prototypeCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Data for the cell, e.g. text for label
id myData = [self myDataForIndexPath:indexPath];

// Prototype knows how to calculate its height for the given data
return [self.prototypeCell myHeightForData:myData];
}

Of course, if you're using custom height, you probably have multiple cell prototypes, so you'd store them in a dictionary or something.

As far as I can tell, the table view doesn't attempt to reuse the prototype, presumably because it was dequeued outside of cellForRowAtIndexPath:. This approach has worked very well for us because it allows the designer to modify cells layouts in the storyboard without requiring any code changes.

Edit: clarified the meaning of sample code and added an example for the case of static height.

UITableViewCell: Custom Row Height for prototype cell

heightForRowAtIndexPath is called (for each row of the table) before the cells are displayed, i.e. before cellForRowAtIndexPath is called. That is necessary to compute the layout of the underlying scrollview, the scroll indicators etc.

How to change the height of a prototype cell in Xcode for tableView?

You can change the height of a cell by adding Constraints to your label inside the cell or otherwise by adding this to your viewDidLoad:

tableView.rowHeight = 40

Or you may add the following delegate function to change the height:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}

Dynamic prototype cell height size of UITableViewController on storyboard

The heightForRowAtIndexPath method is the correct/best way to establish a custom height for a cell in a UITableViewController.

It has some performance problems when table view is large (>1k rows), but it gives you the flexibility to dynamically change the height of your cells and make a prettier user interface.

Custom Cell Row Height setting in storyboard is not responding

On dynamic cells, rowHeight set on the UITableView always overrides the individual cells' rowHeight.

But on static cells, rowHeight set on individual cells can override UITableView's.

Not sure if it's a bug, Apple might be intentionally doing this?

Unable to set the height of a prototype cell as a constraint in storyboard

Actually constraints shouldn't be applied directly to content view of UITableViewCell as they already act as dynamic wrapper and adjust their height according to their subviews.

Try adding a subview to content view and set a height constraint to that subview. And at the same time pin all sides of your new subview to content view.

If you expect a static height from cell, then please override below code and make your viewController implement UITableViewDelegate:

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat

Or if you want your cell height to be calculated from height constraints you just applied to subview of content view, in that case use:

 self.tableView.rowHeight = UITableViewAutomaticDimension;

Row Height of UITableView Custom Cell doesn't update, Swift iOS8

You can update it with override this method in your TableViewController:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}

how do I resize the area of the prototype cell in storyboard

Here's how to change this behaviour:

From the Interface Builder:

  • Select the view controller;
  • Find the Adjust scroll view insets option; and
  • Uncheck it.

How to make fixed height cell on uitableview?

The reason that you are having this issue is you have probably set your Custom tableview cell's row height to 345 but that is not set as custom while your UITableview's row height is less than 345. So, what you need to do is go to storyboard and select the Table(UITableview) and set its row height to the maximum possible row height.

Sample Image

Let's assume that you are going to have two different kind of row heights. One with 345 and another with 325. As 325<345, you set your tableview's row height to 345.

Sample Image

Now, select the custom tableview cell and make its row height as custom and set that to either 345 or 325. In your case, it will be 345.

Sample Image

It should be good now.

However, more appropriate way when you have different cell size would be to use the delegate method for row height specification.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==0){
return 345;
}
else if(indexPath.row==1){
return 325;
}
else{
return 300; //a default size if the cell index path is anything other than the 1st or second row.
}
}


Related Topics



Leave a reply



Submit