2015-11-05 94 views
2

我有UITableView其中我添加了一个UIButton。问题是它只显示在标题的顶部。如何在所有章节标题中添加按钮,以便我可以使该部分交互?UITableView的交互式部分标题

下面是用于添加的UITableView

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 
    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView 
    header.contentView.backgroundColor = UIColor(red: 255/255, green: 200/255, blue: 20/255, alpha: 1.0) // Just some random color 
    header.textLabel!.textColor = UIColor.whiteColor() 
    let btn = UIButton(type: UIButtonType.Custom) as UIButton 
    btn.frame = CGRectMake(header.frame.width - 62, 0, 50, 50) 
    btn.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside) 
    btn.setTitle("Direction", forState: .Normal) 
    btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    header.contentView.addSubview(btn)  
} 

节头按钮谁能告诉我如何添加在每节的互动按钮的代码?

回答

2

你必须使用该委托

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

定制的tableview的headerview部分

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
{ 
    let header: = UIView() 
    let btn = UIButton(type: UIButtonType.Custom) as UIButton 
    btn.frame = CGRectMake(header.frame.width - 62, 0, 50, 50) 
    btn.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside) 
    btn.setTitle("Direction", forState: .Normal) 
    btn.setTitleColor(UIColor.whiteColor(), forState: .Normal) 

    header.addSubview(btn) 
    return header 

} 
+0

是否有任何扩大部分的可能性。当我尝试添加这个时,我无法看到章节标题。 –

0

你可以简单地使用该委托方法泰伯维:

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