swift - 展开 Segue 以重新加载更新的 TableView 单元格

标签 swift uitableview

我的 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/

相关文章:

swift - 添加 Swift 标准库中未实现的 SequenceType

swift - 在for循环中调用异步方法

ios - 滚动时收缩的标题 View

swift - 如何在与单元格关联的 Realm 对象中存储单元格高度值?

ios - 我想在单个 TableView 中使用 nib 中的两个不同的自定义单元格。两个原型(prototype)电池具有不同的高度

ios - 在 Swift 中从 UITableviewControllers 调用时定位 subview

缩小到主页过渡动画期间的 iOS 应用程序图标背景颜色

ios - 更新 tableview 后不起作用。为什么会这样?

iphone - 重新加载特定的uitableview单元格的数据

swift - 如何在 SwiftUI View 中自动反射(reflect) Core Data+iCloud 的变化?