2015-11-05 40 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?