如何在segue之前更新选定的行?

编程入门 行业动态 更新时间:2024-10-09 16:25:54
本文介绍了如何在segue之前更新选定的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-05-01 11:05:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   segue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!