簡體   English   中英

使用自定義標題按鈕來修改tableview中的部分?

[英]Using a Custom header Button to modify a section in tableview?

我正在嘗試提供刪除或向表視圖添加節的功能。 我在筆尖中創建了一個自定義標題視圖,並向其中添加了標簽和2個按鈕。 問題是,在刪除或添加部分時,我無法連貫。 到目前為止,這是我的代碼:

//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...
}

您只能將情節提要場景或NIB連接到其所屬類。

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

在此處輸入圖片說明

將“類”從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
}

暫無
暫無

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

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