1

I have a tableview setup whereby every row has its own custom section header. This is done with a prototype cell in the story board. I have a segue to a view controller on the click event of a row. I achieved this by ctrl dragging on the storyboard.

I want my section header to segue to a different controller, however. I ctrl dragged from my header prototype cell to a view controller on the storyboard also.

The result is that both clicking on the row and clicking on the header of the row are bringing me to the same view controller.

It seems the header is linked to the row. Is there any way around this? Could I give my segues identifiers and override the prepare for seg? Or is it possible to have a separate click event on a section header as the rest of the row?

UPDATE: I have to tried to implement it as follows:

I ctrl dragged from my vc to the destination vc in the storyboard and gave the seg an identifier.

In my viewForHeaderInSection, I have added the following:

let tapper = UITapGestureRecognizer(target: self, action:Selector("seg"))
tapper.cancelsTouchesInView = false
headerCell.addGestureRecognizer(tapper)
return headerCell

my seg func just perform a seg with the identifier I set up as explained above.

func seg(){
        self.performSegueWithIdentifier("feedToMembersDetailed", sender: self)
}

In my overridePrepareForSeg, I check the identifier and call the appropriate method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if (segue.identifier == "feedToMembersDetailed"){
            //getting in here
            getMembersContentFromRow(segue)
        }
        ... extra conditions
    }

and finally my getMembersContentFromRow looks as follows:

func getMembersContentFromRow(segue : UIStoryboardSegue){
        var membersDetailed = segue.destinationViewController as! MembersDetailedController
        if let indexPath = self.feedTableView.**indexPathForSelectedRow()**{
            if let selectedId = self.newsFeed?[indexPath.section].authorId{
                Prefs.followedId = selectedId.toInt()!
            }
            println("setting postAuthorPic as \(self.newsFeed?[indexPath.section].authorPic)")
            membersDetailed.postAuthorPic = self.newsFeed?[indexPath.section].authorPic
            membersDetailed.postAuthorID = self.newsFeed?[indexPath.section].authorId
            membersDetailed.postAuthor = self.newsFeed?[indexPath.section].author

        }
        else{
            //getting in here. I need a method that can retrieve indexPath for me
            println("could not get selected row")
        }
    }
user2363025
  • 6,365
  • 19
  • 48
  • 89

2 Answers2

1

You would need to implement viewForHeaderInSection: method and add a UITapGestureRecognizer to a view which would be retuned as header view. You can then handle the tap on your table section headers to go to different View Controller.

Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • since the header is already seguing to the place that my rows are set up to segue to, will i also have to implement an override shouldPerformSegue func too? – user2363025 Oct 02 '15 at 09:16
  • Why are you not ctrl+drag your section header segue to the VC you want to load on tap on section header? – Abhinav Oct 02 '15 at 09:18
  • this is what I tried first as per the question...but the segue that is linked to my row...is triggering when i click on a section header... for some reason. I'm new to iOS dev, this is why I was unsure if this was the expected behaviour of a row's section header – user2363025 Oct 02 '15 at 09:23
  • See, unlike table rows, section headers are not tappable by default. You to enable them to take user taps. One programmatic way I told you how to go about it. There is another [thread](http://stackoverflow.com/questions/9219234/how-to-implement-custom-table-view-section-headers-and-footers-with-storyboard) which explains about it the storyboard way. Take a look! – Abhinav Oct 02 '15 at 09:26
  • but by default when I click on my section header right now, I am getting the segue that is linked to the row. I have added two ctrl drag segues one for the row nad one for the header. Then in overrideprepareforseg, I am checking the segue.identifier and seguing then based on that, but when I click on a section header, i am getting the wrong identifier i.e. the one linked to my row.. – user2363025 Oct 02 '15 at 09:52
1

Ok here it is, I am not writing code, just the steps you need to follow

  1. Remove the segues you have created from your row and headers. Just assign SegueIdentifier for the controllers to be opened, say SegueIdentifierForRow and SegueIdentifierForSection.

  2. Since there is no action in tableview headers, you need to add a custom action may be an UIButton or, UITapGestureRecognizer. Cell selection you can leave as it is. And to determine section selection you can set tag for each section.

  3. On Row CellSelection manually initialise ViewController by using identifier SegueIdentifierForRow, and same for section using different identifier SegueIdentifierForSection.

Follow above you should be able to do whats required.

Cheers.

iphonic
  • 12,615
  • 7
  • 60
  • 107