0

I just added a button on my custom cell

enter image description here

the button "Look at the reviews", what i would like to do is expand or collapse the cell by tapping this button, i also would like to show other labels when the cell expands. For now to know which cell i'm going to tap i created a tag in my cellForRowAt

cell.reviewButton.tag = indexPath.row
cell.reviewButton.addTarget(self, action: #selector(CourseClass2.ReviewButtonTap(_:)), for: .touchUpInside)

and the IBAction

@IBAction func ReviewButtonTap(_ sender: UIButton) {

      let index2 = IndexPath(row: sender.tag, section: 0)


} 

but i don't know how can i expand or collapse and especially i do not know how to show other elements in the cell when it expands, i already looked around the net but i didn't find useful tutorial, i'm a beginner can someone explain me how to do?

fisherM
  • 177
  • 1
  • 3
  • 14
  • 2
    take a look here: https://stackoverflow.com/q/47963568/2912282 My answer there explains it in enough details. – Milan Nosáľ Jan 11 '18 at 18:09
  • Agreed take a look at @MilanNosáľ answer. You may also want to look into using [Dynamic TableView Cell Heights](https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) , to use along with the stackViews. – Jacob Boyd Jan 11 '18 at 18:14
  • My answer there uses dynamic height already, but yeah, it's good to take a look specifically to that, too. – Milan Nosáľ Jan 11 '18 at 18:17
  • StackViews are the way to go, if you wanna get a better understanding about them, take a look at this tutorial https://www.raywenderlich.com/160646/uistackview-tutorial-introducing-stack-views-2 – lionserdar Jan 11 '18 at 18:21
  • And if you'll find my answer https://stackoverflow.com/a/47963680/2912282 useful, I would appreciate if you leave an upvote :) – Milan Nosáľ Jan 11 '18 at 18:31
  • yes now i'm studying you answer to better understand, a question, you add constraints to the cell also with the storyboard or only the code i see in your answer? – fisherM Jan 11 '18 at 18:34
  • and also how can i add this import PlaygroundSupport ? – fisherM Jan 11 '18 at 18:38
  • I concur with @MilanNosáľ his other question using stack views and dynamic height is the way to go. In honour of this I will award you an upvote. – Upholder Of Truth Jan 11 '18 at 18:40
  • Why do u need import PlaygroundSupport. That is only for playground projects.. I'm pretty sure you have an iOS project with .xcodeproj extension @fisherM – lionserdar Jan 11 '18 at 18:41
  • ah ok understand – fisherM Jan 11 '18 at 18:41
  • @fisherM my example there is a full working solution, you don't need any storyboards for it. if you want to use storyboards, just create `@IBOutlet`s of the constraints and views you need to manage – Milan Nosáľ Jan 11 '18 at 18:43
  • understand, thank you – fisherM Jan 11 '18 at 18:46

1 Answers1

0

I don't know what is the best approach to do it. But I will create headers instead of cells and when the button "Look at the reviews" pressed, I will add and remove a cell respect to that header.

Check this tutorial: https://newfivefour.com/swift-ios-expanding-uitableview-sections.html.

In numberOfRowsInSection you can decide expand/collapse action respect of that header.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if hidden[section] {
        return 0
    } else {
        return 1
    }
}
Chandan kumar
  • 1,074
  • 12
  • 31
  • Using headers and adding/removing cells will only increase the complexity. I don't thin this is the best approach, if you use UIStackViews you can easily set the isHidden flag to true/false and display any extra labels... I don't think this question requires such complexity! – lionserdar Jan 11 '18 at 18:42