2016-10-02 3 views
0

私はテーブルビューにセクションを削除または追加する機能を提供しようとしています。 nibでカスタムヘッダービューを作成し、ラベルと2つのボタンを追加しました。 問題は、削除またはセクションを追加する際にドットを接続できないということです。 これは、これまでの私のコードです:カスタムヘッダーボタンを使用して、tableviewのセクションを変更しますか?

//Setup Header 
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerID) as! CustomHeader 
    //Configure header... 
    header.addBtn.addTarget(self, action:Selector(("add")), for: .touchUpInside) 
    header.addBtn.tag = section 

    switch section { 
    case 0: 
     header.sectionLBL.text = "Detail 1" 
    case 1: 
     header.sectionLBL.text = "Detail 2" 
    default: 
     break 
    } 
    return header 
} 

@IBAction func addButton(sender: UIButton) { 
    //Add section code here... 
} 

@IBAction func delete(sender:UIButton){ 
    //delete section code here... 
} 

私も、私はボタンで動作するようにコミットスタイルを取得する方法を把握しようとしていた。

//Setup Editing Style for table view 
    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(true) 


    } 

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
//Editing styles for deletion or insertion... 
} 

答えて

1

あなただけのストーリーボードシーンやニブを接続することができます彼らの所属するクラスに。

1)CustomHeaderクラスにプロトコルを追加し、クラスがNIBに割り当てられている保証:IBのインスペクタで新しいクラスへ

protocol CustomHeaderButtonPressDelegate { 
    func didPressAdd() 
    func didPressDelete() 
} 

class CustomHeader: UIView { 
    var delegate:CustomHeaderButtonPressDelegate? 
    @IBAction func addButton(sender: UIButton) { 
     delegate?.didPressAdd() 
    } 

    @IBAction func delete(sender:UIButton){ 
     delegate?.didPressDelete() 
    } 
} 

2)を割り当て、彼NIBは

enter image description here

「クラス」をUIViewからCustomHeaderに変更します。

3)プログラム的CustomHeaderIBAction

4)を設定し、デリゲート

class YourClass: UITableViewController, CustomHeaderButtonPressDelegate { 
... 
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerID) as! CustomHeader 
    header.delegate = self 
... 
} 
... 
func didPressAdd() { 
    //handlePress 
} 
func didPressDelete() { 
//handlePress 
} 
に、ボタンのターゲットを接続します
関連する問題