1

I'm practicing with TableViewController and custom TableViewCell. I have made a question list & after clicking Show button it shows the question's options like this.

enter image description here

but removing spacing of question list view from above image I have wrote this code

-(void)viewWillAppear:(BOOL)animated{
    self.tableView.estimatedRowHeight = 40.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;}

and the output comes like this before and after clicking Show button.

enter image description here

but I'm trying to make my output with dynamically manageable the row height after clicking the Show button.

Constraints :

enter image description here

NOTE: I have gone through Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

But haven't resolved my problem.

Thanks.

Community
  • 1
  • 1
R.A.Munna
  • 1,699
  • 1
  • 15
  • 29

1 Answers1

2

You constraints are not proper. And instead of using rowHeight property use heightForRow method because this property called once but this method is called for each and every cell.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}

For constraints your every component in cell must have top, bottom and height. For anyone of the component, if you missed anyone of this constraint then there is defect is created in your UI.

For more details post your prototype-cell screen shot and show the constraints you have given to them.

Edit

Tick on clips to bounds property of StackViewand its inner four button.

enter image description here

After doing this I get following output :

enter image description here

Reason:

StackView not let the inner content to be 0.

If you still have doubt I have demo you can take it from me.

There is one more approach for this type of problems

  1. Create two custom cells,one for question and other for options.
  2. Initially, load cell question.
  3. If user clicks on show button get the indexpath of button and load option cell in the table just after that indexpath.
  4. If user clicks on hide button then remove that option cell.

NOTE:

  1. If one option cell can be load at a time in the tableView then maintain the button selected option in a variable.
  2. If multiple option cell can be load at a time then use array to maintain them.
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • Thanks for your replay. I'm using `xcode 7.0` and `iOS 9.0` and can't find the `heightForRow` property. If you kindly explain I'll be grateful. – R.A.Munna Jan 23 '17 at 05:45
  • OK post screen shot of your UI and constraints as I said in last lines of my answer. – dahiya_boy Jan 23 '17 at 05:49
  • `rowHeight` always works for me, the only issue can be the constraints. – Bista Jan 23 '17 at 05:56
  • here is my constraints and I have used stackview. https://i.stack.imgur.com/SkKYa.png – R.A.Munna Jan 23 '17 at 06:01
  • Post constraints of Options. – dahiya_boy Jan 23 '17 at 06:09
  • @Mr.Bista yeah! you are right. but if your cell is more dynamic then methods doesn't work. And `UITableViewAutomaticDimension` method get the height from the storyboard during the the executions, once it takes height it returns same for all the cells at every situation. but here it doesn't work, hope you get me. Thats why I never suggest to use tis property because half of ppl go mad to know the differ even they start argu because their code is working in some cases. so be habitual of best coding. But you'r also right. I liked it. (y) – dahiya_boy Jan 23 '17 at 06:13
  • @ the_dahiya_boy, thanks for supporting me and I have designed prototype cell using `stackview` of Options. So, here I only set the `content hugging priority` and `content compression resistance priority`. If you want those value I can provide. – R.A.Munna Jan 23 '17 at 06:27
  • @ the_dahiya_boy, here is `content hugging priority` and `content compression resistance priority` of **questions Stack view** https://i.stack.imgur.com/SAK8X.png and **Options Stack view** https://i.stack.imgur.com/P9KS2.png and image title represents the `view`. – R.A.Munna Jan 23 '17 at 07:10
  • Have you set top and bottom constaraints for stackview? – Bista Jan 23 '17 at 07:53
  • @Mr.Bista, I have only added **question** and **options** stacks top and bottom constraints. – R.A.Munna Jan 23 '17 at 08:09
  • @the_dahiya_boy, thanks for your nice support. your contribution help me a lot :) . – R.A.Munna Jan 23 '17 at 11:45
  • @R.A.Munna So up-vote it so that it helps for others too. – dahiya_boy Jan 23 '17 at 11:46