2017-01-15 7 views
1

私はできない簡単なことに夢中になります。tabeviewヘッダーを表示します。ダイナミックテーブルビューでSwift 3.0を使用しています。このテーブルビューでは、ユーザーが連絡先リストを追加できる連絡先ディレクトリを作成しています。 ダイナミックプロトタイプのコンテンツを含むストーリーボードで私のテーブルビューを宣言します。プログラム的に私は私の連絡先のリストを持って、私のリストが私のテーブルビューで適切に表示されるために必要なことをやります。TableViewに表示ヘッダーを表示

その後、私は1つのセクションを宣言し、次の方法で使用した:私は私の連絡先リスト上のいくつかのアクションを実行するボタンを追加ヘッダで

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 

      return 50.0 
} 

を:

にfuncのtableView(のtableView:のUITableView、 viewForHeaderInSectionセクション:Int) - > UIView? {

let frame: CGRect = tableView.frame 
    let DoneBut: UIButton = UIButton(frame: CGRect(x: frame.size.width - 200, y: 0, width: 150, height: 50)) 
    DoneBut.setTitle("Done", for: .normal) 
    DoneBut.backgroundColor = UIColor.red 



    DoneBut.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) 

    DoneBut.backgroundColor = UIColor.blue 


    let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)) 
    headerView.backgroundColor = UIColor.red 
    headerView.addSubview(DoneBut) 

    return headerView 


} 

=>結果はヘッダーのみ表示されます。ヘッダーにできるアドバイスはありますか?

答えて

1

heightForHeaderInSectionviewForHeaderInSectionの方法を少し修正する必要があります。 両方のメソッドの最初の括弧の後に、両方のオーバーライドと '_'を追加する必要があります。 (下記参照)

このコードでは、セクションヘッダーが表示されます。

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
     return 50.0 
    } 

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let frame: CGRect = tableView.frame 
    let DoneBut: UIButton = UIButton(frame: CGRect(x: frame.size.width - 200, y: 0, width: 150, height: 50)) 
    DoneBut.setTitle("Done", for: .normal) 
    DoneBut.backgroundColor = UIColor.red 



    DoneBut.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) 

    DoneBut.backgroundColor = UIColor.blue 


    let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 50)) 
    headerView.backgroundColor = UIColor.red 
    headerView.addSubview(DoneBut) 

    return headerView 
} 
+0

偉大なthxsは、チャームのように働いています!!!!! – Mick

関連する問題