我的 TableView 有一个“添加”按钮,您单击添加按钮,一个新的 View Controller 会以模式方式打开,您输入文本字段(名称、位置、评级等...),然后单击保存它会将您带到玩家个人资料页面,其中包含您在“添加 View Controller ”中输入的所有信息。我还在玩家个人资料页面上有一个 Unwind Segue,可让您返回到列出所有玩家的表格 View 你添加了。我的问题是,当我单击展开回桌面 View 的按钮时,它不会添加新玩家。IE 如果我的桌面 View 中有 3 个玩家要启动,我单击“添加”以添加第四个玩家,输入信息并点击保存,然后进入玩家个人资料页面,当我点击展开按钮时,表格 View 仍然列出 3,而不是 4。下面是我的展开代码...
@IBAction func unwindToRecipeList(segue: UIStoryboardSegue) {
if let sourceViewController = segue.sourceViewController as? RecipeDetailViewController, recipe = sourceViewController.recipe {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
recipes[selectedIndexPath.row] = recipe
tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
} else {
let newIndexPath = NSIndexPath(forRow: recipes.count, inSection: 0)
recipes.append(recipe)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
}
最佳答案
将配方添加到配方后,尝试使用 tableView.beginUpdates()
和 tableView.endUpdates()
:
@IBAction func unwindToRecipeList(segue: UIStoryboardSegue) {
if let sourceViewController = segue.sourceViewController as? RecipeDetailViewController, recipe = sourceViewController.recipe {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
recipes[selectedIndexPath.row] = recipe
tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
} else {
recipes.append(recipe)
tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: recipes.count-1, inSection: 0)], withRowAnimation: .Automatic)
tableView.endUpdates()
}
}
}
关于swift - 展开 Segue 以重新加载更新的 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36994459/