问题
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")
}
}
回答1:
Ok here it is, I am not writing code, just the steps you need to follow
Remove the segues you have created from your row and headers. Just assign SegueIdentifier for the controllers to be opened, say SegueIdentifierForRow and SegueIdentifierForSection.
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 settag
for each section.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.
回答2:
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.
来源:https://stackoverflow.com/questions/32903847/separate-segue-on-section-header-as-on-table-view-row