0

I have a UITableView with an interactive section header created with a custom action using an UIButton. When this button is pressed a segued is performed to another View Controller via prepareForSegue. However, the section selection is not available in this function. Is there a solution to make a section index available? Since section is already available in viewForHeaderInSection, could it in a way be passed to prepareForSegue?

There has been another thread with similar subject, but I am not able to determine the section index.

Thanks in advance, Gerard

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    let header = UIView()
    let btn = UIButton(type: UIButtonType.Custom) as UIButton
    btn.frame = CGRectMake(0, 0, tableView.frame.size.width, 40)
    btn.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
    btn.setTitle(String(section), forState: .Normal)
    btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
    header.addSubview(btn)
    return header
}

func buttonAction(sender:UIButton!)
{
    self.performSegueWithIdentifier("myIdentifier", sender: self)
}

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

    if segue.identifier == "myIdentifier" {
        let detailViewController = segue.destinationViewController as! MyViewController

        if let selectedSection = sender {
            // Here the selected section should be determined....
            // ... so that data can be passed to detailViewController, e.g.
            //    detailViewController.sectionNumber = section
        }
}
Community
  • 1
  • 1
Gerard
  • 349
  • 2
  • 16

1 Answers1

0

This is what you can possibly do add a variable var sectionToPass: Int

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    let header = UIView()
    let btn = UIButton(type: UIButtonType.Custom) as UIButton
    btn.frame = CGRectMake(0, 0, tableView.frame.size.width, 40)
    btn.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
    //Add This Part
    btn.tag = section
    //
    btn.setTitle(String(section), forState: .Normal)
    btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
    header.addSubview(btn)
    return header
}

func buttonAction(sender:UIButton!)
{
    //ADD THIS PART
    sectionToPass = sender.tag
    //
    self.performSegueWithIdentifier("myIdentifier", sender: self)

}

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

if segue.identifier == "myIdentifier" {
    let detailViewController = segue.destinationViewController as! MyViewController

    if let selectedSection = sender {
        // Here the selected section should be determined....
        // ... so that data can be passed to detailViewController, e.g.
        //    detailViewController.sectionNumber = section
        //ADD THIS PART
        detailViewController.sectionNumber = sectionToPass
    }
}
Shabarinath Pabba
  • 1,359
  • 2
  • 13
  • 22