2017-02-20 89 views
0

''Hello World'':) 我有一個包含4個不同部分的UItableview。我試圖根據選擇的部分填充每個部分的行。我掙扎 -__-檢測選擇哪個部分,以便相應地填充它們。在我的程序中,我創建了4個uibutton(在tableview之外,它們代表每個用戶;當選擇一個按鈕時,我使用它們來命名節頭)。現在我想鏈接用該部分選擇的按鈕。 如何進一步瞭解選擇哪個部分。如果選擇了按鈕A,則表示選擇了部分0,然後在部分0中執行操作x(例如,插入新行)。 如果選擇按鈕B(即選擇部分1),然後做了手術y和等等...已選擇UItableview部分 - swift

預先感謝幫助一個絕望的靈魂

回答

0

設置標籤按鈕。

button1.tag = 0 
button2.tag = 1 
button3.tag = 2 
button4.tag = 3 

再加入目標

button1.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside) 
button2.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside) 
button3.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside) 
button4.addTarget(self, action: #selector(didTouchButton(_:)), for: .touchUpInside) 

那麼當按鈕可以選擇檢查發件人標籤,並做部分操作

func didTouchButton(_ sender: UIButton) { 
    let section = sender.tag 

    //do operations in section 
} 

Here是插入和刪除在部分行的例子基於部分指數。在Swift3中,您應該基於以下方法:

func insertRows(at: [IndexPath], with: UITableViewRowAnimation) 
func deleteRows(at: [IndexPath], with: UITableViewRowAnimation)