在交互式 table 视图 header 中确定部分索引

Determining section index in an interactive table view header

我有一个带有交互部分的 UITableView header 通过使用 UIButton 的自定义操作创建。当按下此按钮时,将通过 prepareForSegue 执行到另一个视图控制器。但是,此功能不提供部分选择。是否有使部分索引可用的解决方案?由于部分已经在 viewForHeaderInSection 中可用,能否以某种方式将其传递给 prepareForSegue?

还有一个 有类似的主题,但我无法确定章节索引。

提前致谢, 杰拉德

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
        }
}

这是你可能做的添加变量 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
    }
}