使用UIAlertController按钮将自定义单元格添加到tableView

时间:2016-12-01 10:27:34

标签: ios swift uitableview uialertcontroller

我创建了一个UITableView并创建了一些单元格并给它们标识符。我还创建了一个UIAlertController,以便在同一个View Controller上按下按钮时显示弹出窗口。我希望能够使用适当的单元格向表中插入新行,具体取决于用户在“警报”弹出窗口中点击的按钮。 帮助

2 个答案:

答案 0 :(得分: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)

我希望它能帮到你

答案 1 :(得分:2)

您可以按照部分进行更改。

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