swift - 在使用 unwind segue 时,如何将从被调用 View Controller 获取的数据显示到调用 View Controller 的动态 tableviewcell 中?

标签 swift

我有动态表格 View ,其中一个单元格(持续时间)在点击时会打开另一个 View Controller ,它是持续时间列表(30 分钟、1 小时、2 小时等)。选择其中一个持续时间后,应在第一个 View Controller 中显示所选的持续时间。我可以使用 unwind segue 将数据传递回第一个 View Controller ,但无法显示传递的值。不知道缺少什么。 我正在显示下面的代码: 第一个 View Controller (调用)

@IBAction func unwindWithSelectedDuration(segue:UIStoryboardSegue) {
    var cell = tableView.dequeueReusableCellWithIdentifier("durationCell") as! durationTableViewCell
    if let durationTableViewController = segue.sourceViewController as? DurationTableViewController,
        selectedDuration = durationTableViewController.selectedDuration {
            cell.meetingDurationCell.text = selectedDuration
            duration = selectedDuration
    }

第二个 View Controller (已调用)

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SaveSelectedDuration" {
        if let cell = sender as? UITableViewCell {
            let indexPath = tableView.indexPathForCell(cell)
            if let index = indexPath?.row {
                selectedDuration = durationList[index]

            }
        }
    }
}

最佳答案

tableView.dequeueReusableCellWithIdentifier 只能在 tableView:cellForRowAtIndexPath: 内调用。在这个上下文之外它没有任何用处。

最简单的解决方法是在存储选定的持续时间后重新加载表格:

@IBAction func unwindWithSelectedDuration(segue:UIStoryboardSegue) {
    if let durationTableViewController = segue.sourceViewController as? DurationTableViewController {
        selectedDuration = durationTableViewController.selectedDuration
        tableView.reloadData()
    }
}

请注意,这假设您整个表只需要一个 selectedDuration,而不是每行一个。如果您每行需要一个,我假设您将它们存储在数组中的某个位置,因此应该在 reloadData 之前更新该数组。

关于swift - 在使用 unwind segue 时,如何将从被调用 View Controller 获取的数据显示到调用 View Controller 的动态 tableviewcell 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36073508/

相关文章:

ios - 在 Swift 中注册远程通知时未收到设备 token

php - Swift 字符串(格式 :array:) joins strings instead of composing them

swift - NSManagedObject 子类模拟

ios - 如何在 iOS 上的给定日期执行后台任务?

swift - 列表需要 3 个元素,但我不知道第三个元素从哪里来

xcode - 非泛型 Swift 类可以实现 UIPickerViewDataSource 但泛型版本不能

ios - 使用位置管理器时,我的 Swift 应用程序耗电非常快

swift - 变量仅在 sendAsynchronousRequest 中更新

ios - 带有数组错误的 Swift NSCountedSet 初始化?

swift - "Protocol ... can only be used as a generic constraint because it has Self or associated type requirements"是什么意思?