R.A.Munna
R.A.Munna

Reputation: 1709

Dynamically manage TableViewCell Height by clicking UIButton

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.

Upvotes: 1

Views: 158

Answers (1)

dahiya_boy
dahiya_boy

Reputation: 9503

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.

Upvotes: 2

Related Questions