我有动态表格 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/