2016-12-01 74 views
3

我已經創建了一個UITableView並創建了一些單元格並給出它們的標識符。我還創建了一個UIAlertController,以便在同一個View Controller上按下按鈕時顯示一個彈出窗口。我希望能夠根據用戶在Alert彈出窗口中點擊哪個按鈕,將適當的單元格插入到表格中。 有幫助嗎?使用UIAlertController按鈕來添加自定義單元格到一個表格

+0

當您從「UIAlertController」註冊新的單元格內容時,將它添加到您的數組/字典中,或者作爲您的UITableView數據源。並調用'yourTableView.reloadData()' – Larme

回答

3

你可以嘗試這樣的方式

let alert = UIAlertController(title:"Test", message: "Message", preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "Add Row", style: UIAlertActionStyle.default, handler: { (action) -> Void in 
     tblView.insertRows(at: [IndexPath], with: UITableViewRowAnimation.left) 
     And update your data Sourse 
    })) 
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)) 
    self.presentViewController(alert, animated: true, completion: nil) 

我希望它會幫助你

2

您可以按部分變更。

self.tblView.beginUpdates() 
self.tblView.insertRows(at: [IndexPath.init(row: self.yourArray.count-1, section: 0)], with: .automatic) 
self.tblView.endUpdates() 
相關問題