【问题标题】:getting the current indexPath of UITableview in another Viewcontroller在另一个 Viewcontroller 中获取 UITableview 的当前 indexPath
【发布时间】:2021-08-22 19:11:35
【问题描述】:

我有一个UITableView,当点击每个单元格时,它会加载详细信息页面,其中名称和副标题是从tableview单元格名称和副标题中加载的。

为此,我做了以下操作

  1. 声明了 var 类型为 Int 的行,在 didSelectRowAt 方法中,indexPath 被传递给此变量

2.Destination 有另一个变量 rownum,它被赋值为

`destination1.rownum = row` 

在 prepare(for segue: UIStoryboardSegue, sender: Any?)方法

我的问题是,当我单击第一个单元格时,rownum 的值 = 0 但这保持不变,在下一次单击单元格时,它会显示第 0 个值本身。

同样它只显示以前的值

加载所有这些值后,如何重置此 rownum/ 行?

【问题讨论】:

    标签: ios swift uitableview viewcontroller


    【解决方案1】:

    您不需要额外的变量。

    • 如果转场的起点是单元格,那么单元格会在sender 参数中传递,您将获得索引路径

      let indexPath = tableView.indexPath(for: sender as! UITableViewCell)!
      
    • 如果segue的来源是控制器,并且您在didSelectRow中调用performSegue,则您自己在sender参数中传递indexPath。然后用

      获取prepare(for中的索引路径
      let indexPath = sender as! IndexPath
      

    如果只有一个 segue 力解包,则值很好,因为 segue 是硬连接。如果有多个 segue,您最好安全地打开 sender。

    【讨论】:

    • 谢谢!!!有效。第一个是我的情况。在我尝试 switch-case 和 if else 条件之前。但这在一行中解决了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2020-06-27
    相关资源
    最近更新 更多