2016-10-02 68 views
0

我試圖給予能夠刪除或添加一個部分到表視圖。 我在筆尖中創建了一個自定義標題視圖,併爲其添加了一個標籤和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:

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到新的類中的IB檢查

enter image description here

將「類」從UIView更改爲CustomHeader

3)您的按鈕目標連接到CustomHeader小號IBAction小號

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 
} 
相關問題