Ega Setya Putra Ega Setya Putra - 1 year ago 82
Swift Question

How to make fixed height cell on uitableview?

So I want to make timeline content like instagram, I'm using custom cell on my uitableview. My problem is I already set the cell height to 345 on the storyboard but I get cell table like below:


and here is my custom cell on storyboard:


How can I fix it, so I can get the result like on my storyboard?

Answer Source

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.

enter image description here

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.

enter image description here

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.

enter image description here

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 {
      return 345;
   else if(indexPath.row==1){
      return 325;
      return 300; //a default size if the cell index path is anything other than the 1st or second row.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download