2016-12-01 71 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() 
相关问题