Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

celltablescreenshot

and here is my custom cell on storyboard:

storyboardss

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

like image 869
Ega Setya Putra Avatar asked May 07 '15 06:05

Ega Setya Putra


2 Answers

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 {
   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.
   }
}
like image 114
Natasha Avatar answered Oct 12 '22 13:10

Natasha


In your UITableViewController subclass, set self.tableView.rowHeight = 345. Might be a bug with storyboard heights not being translated.

like image 40
Schemetrical Avatar answered Oct 12 '22 12:10

Schemetrical