簡體   English   中英

UITableView 的交互部分標題

[英]Interactive Section header of UITableView

我有UITableView在其中添加了一個UIButton 問題是它只顯示在標題的頂部。 如何在所有部分標題中添加按鈕,以便使該部分具有交互性?

這是在UITableView Section Header 上添加按鈕的代碼

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

誰能告訴我如何在每個部分添加交互式按鈕?

你必須使用這個委托

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

}

您可以簡單地使用 Tableview 的這個委托方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM