确定交互式表视图标题

时间:2015-11-12 20:44:55

标签: swift uitableview segue sections

我有一个UITableView,其中包含使用UIButton自定义操作创建的交互式节标题。按下此按钮时,将通过prepareForSegue对另一个View Controller执行segued。但是,此功能中不提供节选择。是否有解决方案可以提供部分索引?由于section已在viewForHeaderInSection中可用,是否可以将其传递给prepareForSegue?

还有另一个thread具有相似的主题,但我无法确定部分索引。

提前致谢, 杰拉德

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

1 个答案:

答案 0 :(得分:0)

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