问题描述
限时送ChatGPT账号..我需要将一个包含一个视图中所选行索引的变量传递给下一个视图.
I need to pass a variable containing the index of the selected row in one view to the next view.
firstview.swift
firstview.swift
var selectedRow = 0;
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedRow = indexPath.row + 1
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "viewAssignmentsSegue") {
let navController = segue.destinationViewController as! UINavigationController
let controller = navController.viewControllers[0] as! AssignmentsViewController
print(selectedRow)
controller.activeCourse = selectedRow
}
}
My issue is that, when a row is selected, the selectedRow
variable isn't updated by the tableView
method before the segue occurs, meaning that the value is essentially总是落后于它应该是什么.我怎样才能延迟 prepareForSegue 直到变量被更新,或者我怎样才能成功地将选定的行毫无延迟地传递到下一个视图?
My issue is that, when a row is selected, the selectedRow
variable isn't updated by the tableView
method before the segue occurs, meaning that the value is essentially always one behind what it should be. How can I delay the prepareForSegue until the variable is updated or how else can I successfully pass the selected row to the next view without delay?
推荐答案
一种可能:不实现 didSelectRowAtIndexPath:
.只需将该功能移动到您的 prepareForSegue
实现中即可.毕竟,这是响应您点击单元格时首先调用的内容.即使在 prepareForSegue
中,你也可以询问 table view 选择了哪一行.
One possibility: Don't implement didSelectRowAtIndexPath:
. Just move that functionality into your prepareForSegue
implementation. That, after all, is what is called first in response to your tapping the cell. Even in prepareForSegue
you can ask the table view what row is selected.
另一种可能性:实现 willSelectRowAtIndexPath:
而不是 didSelectRowAtIndexPath:
.它发生得更早.
Another possibility: Implement willSelectRowAtIndexPath:
instead of didSelectRowAtIndexPath:
. It happens earlier.
这篇关于如何在segue之前更新选定的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论