以编程方式创建的按钮不响应目标,swift

时间:2015-03-28 19:47:56

标签: swift button header tableview frame

我在第1部分的tableView标题中创建了一个按钮。

我是这样做的:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let header = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
        header.backgroundColor = UIColor.greenColor()
    if section == 1 {
        var button   = UIButton()
        button.frame = CGRectMake(100, 100, 100, 50)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle("Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
        header.addSubview(button)
    }
        return header
}

 func Action(sender: UIButton){
    println("button was pressed")
}

该按钮仅出现在第1部分(应该如此),但是如果按下它而不是打印信息,则没有任何反应。

1 个答案:

答案 0 :(得分:0)

我的问题是我没有正确指定button.frame。由于按钮位于框架之外,因此从未调用过。

 button.frame = CGRectMake(5, 2, tableView.frame.size.width - 5, 18);
相关问题