0

I have a tableview header within my table view that uses a custom cell with a segment control in it.

I have added a gesture recognizer to my storyboard:

@IBAction func swipeLeftTapped(_ sender: Any) {
} 

I want to swipe left and decrease the selected segment of the segment control within that storyboard header by 1. How would I do that. Here is what I have tried but obviously it doesn't work.

let header = self.mainTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! HeaderTableViewCell

also

let header2 = self.mainTableView.headerView(forSection: 0) as! HeaderTableViewCell

header.productSegmentControl.selectedSegmentIndex += 1

Here is my code for the tableview header

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderTableViewCell
    cell.productSegmentControl.selectedSegmentIndex = productIdIndex
    return cell
}

How do you access a tableview header like you would a normal cell?

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
Nevin Jethmalani
  • 2,726
  • 4
  • 23
  • 58

1 Answers1

0

Why you are using a cell for the header, just use a normal view and add whatever you want to this view, and then you can access it's subViews.

check Martin's answer here

MhammedMohie
  • 312
  • 2
  • 9
  • so I did that and got it to work. but now the problem is that I am not able to get the action from the segment when it changes. Before I was using this `@IBAction func productSegmentControlChanged(_ sender: UISegmentedControl) {}` in my main view controller. how would I access the tap of the segment control now that it is in a view? – Nevin Jethmalani Mar 27 '18 at 21:03
  • Try to implement your view by code and the action selector too – MhammedMohie Mar 28 '18 at 01:28