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

Using UIAlertController buttons to add custom cells to a tableView

我创建了一个 UITableView 并创建了一些单元格并为它们指定了标识符。我还创建了一个 UIAlertController 来在同一个 View Controller 上按下按钮时显示弹出窗口。我希望能够根据用户在警报弹出窗口中点击的按钮,使用适当的单元格将新行插入 table。 有帮助吗?

你可以这样试试

    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)

希望对你有所帮助

您可以根据部分进行更改。

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