0

I have a textLabel in table view header section in a Question/Answer app. When the user tap on Question, answer is shown and on tap again answer will hide.

I am displaying Question in header section but if the question text is long the text is not getting adjusted nor is it wrapping into more lines.

belwood
  • 3,320
  • 11
  • 38
  • 45

1 Answers1

0

Though your question is not clear enough but so far I have understood that you want to make a UITableView which is Group TableView. If I am not wrong then you want to make every section as a question container and Row or Cell will be the answer of that question. In one word you want to make an expandable UITableViewCell. If so then you can check following link

https://www.appcoda.com/expandable-table-view/

https://medium.com/ios-os-x-development/ios-how-to-build-a-table-view-with-collapsible-sections-96badf3387d0

https://mobikul.com/create-expandable-table-view-swift/

Now your second problem was HeaderText which is not taking numberOfLines .To solve this problem you can use the delegate method of UITableView which is func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? where you can add your UILabel and set your numberOfLines of the text.

AND func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat which will contain the height of the UITableView section header. For making dynamic header you have to calculate the UILabel height and set it as a section header height.

Feel free to ask any questions.

Happy Coding!!

Emon
  • 452
  • 3
  • 11
  • 21
  • Thanks for your answer!! but if I add an label on viewForHeaderInSection than tap gesture on section stops working and if I take tap on label than also its not working.As I want to expand the cell when I tap on section.Pls help!! – Aadhar Mathur Apr 10 '19 at 09:36
  • https://github.com/jeantimex/ios-swift-collapsible-table-section Please follow every single step of this code and I thing it should work. – Emon Apr 11 '19 at 05:33
  • Or In spite of using label you can add button which will contain your label text and make one function of that button action which will receive the index of the cell as a parameter so that you can understand which cell is active. – Emon Apr 11 '19 at 05:43