0

I am working on dynamic cell in my tableView and give it these two function:

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewAutomaticDimension;
}

And I pin my label to the top and bottom of the cell.

The conflict is: When I run the code for the first time there is no dynamic cell and give wrong height.

When I'm scrolling down and return it convert to dynamic cell.

I used:[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:YES]; in the function: (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath

Can anyone explain what happened to my view?

Image before scrolling

Image after scrolling

Vegetacoding
  • 59
  • 1
  • 12
  • I think You are getting table data in ViewDidload method and reloading table view in viewDidLoad. So get the array in separate method and call that method after 0.1 seconds from viewDidLoad and reload it. – phani Oct 11 '18 at 13:13
  • I send a get request. When the request come back and handle I call reloadData. I call it in a separate method. – Mohammed Ali Khaled Oct 11 '18 at 13:47
  • Change estimatedHeightForRowAtIndexPath 100 to UITableViewAutomaticDimension – Chirag Kothiya Oct 12 '18 at 04:58
  • You use UITableViewAutomaticDimension when you want to tell the cell the height is not calculated yet, and it should use with heightForRowAtIndexPath. estimatedHeightForRowAtIndexPath you need to put the estimated height "not correct once" so it will not change. – Mohammed Ali Khaled Oct 14 '18 at 05:36

2 Answers2

0

Try to change estimateHeight number change to bigger number. For exmaple 300.

User18474728
  • 363
  • 2
  • 11
  • estimatedHeightForRowAtIndexPath you use it to put the estimated height "not correct once" so it will return the same. – Mohammed Ali Khaled Oct 14 '18 at 05:38
  • How about reload tableView in viewDidLoad? After init all items redraw tableView again at the last time. – User18474728 Oct 14 '18 at 06:12
  • I used it. and i use function **reloadRowsAtIndexPaths** in dequeueReusableCell Before i return cell. But still give me the same result :( . – Mohammed Ali Khaled Oct 14 '18 at 07:16
  • 1
    How did you use dequeueReusableCell?? And also try this. Before return your cell, call cell.layoutIfNeeded() in cellForRowAtIndexPath – User18474728 Oct 14 '18 at 13:53
  • - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { UserCell *cell = (UserCell *)[self.tableView dequeueReusableCellWithIdentifier:@"userCell"]; // define user User *user = [[User alloc]init]; // if there is no data if (self.users.count == 0) { return cell; } user = self.users[indexPath.row]; // define new cell of type UserCell UserCell *userCell = (UserCell *) cell; // update cell UI [userCell updateUI:user]; return cell; } – Mohammed Ali Khaled Oct 14 '18 at 14:51
  • above the function dequeueRuesableCell. I will try **cell.layoutIfNeeded()** – Mohammed Ali Khaled Oct 14 '18 at 14:52
  • Finally it's work :D. And i didnt use ** layoutIfNeeded** function. – Mohammed Ali Khaled Oct 14 '18 at 14:53
  • thanx bro, hope the best to you ;) . – Mohammed Ali Khaled Oct 14 '18 at 15:00
0

"The conflict is: When I run the code for the first time there is no dynamic cell and give wrong height.

When I'm scrolling down and return it convert to dynamic cell."

--if you want same height on first run and after scrolling the tableview , set the same height value on both delegate functions -> estimatedHeightForRowAtIndexPath, heightForRowAtIndexPath

Jerry
  • 21
  • 2
  • I want it dynamic not a fixed height. Estimated used to give approximate not correctly height. – Mohammed Ali Khaled Oct 16 '18 at 13:41
  • dynamic means, set height with respective to text size ? – Jerry Oct 16 '18 at 14:28
  • yup. You need to use it when you have un estimated height. For example, if you have label and you need to assign to its text a value. And this value might be 3 lines or 5 or just one word. you can use dynamic cell in this case. **Dynamic** will give cell height from label height. To achieve this you need to bind the top and bottom of the **Label** to top and bottom of the **cell** and give **Label numberOfLines = 0** – Mohammed Ali Khaled Oct 16 '18 at 14:34
  • have you checked this link: https://stackoverflow.com/questions/37022387/ios-dynamic-height-for-uitableviewcell ? – Jerry Oct 16 '18 at 14:39
  • Yup. I do it correctly. But i dont know why it wasn't working. I fixed the problem, the problem is the cell need to reload it after you add content to it. – Mohammed Ali Khaled Oct 16 '18 at 14:50