【问题标题】:How can I get the index path.row of first UITableView in the Second UITableView using Swift3如何使用 Swift3 在​​第二个 UITableView 中获取第一个 UITableView 的索引 path.row
【发布时间】:2017-01-04 12:17:41
【问题描述】:

我有两个UITableViews。第一个UITableView 包含一个数据数组,第二个是空的。如何获取第一个表的索引路径以便在第二个表中使用它来查看不同的数据?这是我想在第二个表中做的一个例子:

这是在第二个表中,就像这个函数一样,我想获取第一个表的indexPath.row

  func loadData() {

      if indexPath.row == 0 { // (of first table)

      // add code

      } else if indexPath.row == 1 { // (of first table)

       // add code

     }
 }

【问题讨论】:

  • 请详细说明,你的问题不清楚,第一个表的索引路径到第二个是什么意思??
  • 我想获取第一个表视图的点击行的索引,以便在第二个表视图的下面的函数中使用它。
  • 您可以为每个 indexPath 设置一个标签值,然后在点击这些行时,可以检索这些标签并将其传递给 loadData() 以加载相应的值或一组值。标记值应该是唯一值。
  • 您需要将第一个表视图索引存储到一个变量中,并将该变量作为属性传递给第二个视图控制器,然后您就可以轻松地做到这一点。每次选择第一个表行时,您都会更新此变量值并且一次你去下一个视图控制器传递这个值。
  • 请您写下答案中的代码吗?因为我是 Swift 的新手。谢谢

标签: ios uitableview swift3 nsindexpath


【解决方案1】:

实现didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    let selctedIndex = indexPath.row
    self.setIndexToSecondTableView(selctedIndex) //Create your function to get selected index of first table view
}

【讨论】:

  • @Zaid Mustafa 请查看上面的答案,如果您需要任何其他说明,请告诉我
  • 你是什么意思(创建你的函数来获取第一个表视图的选定索引)你能写一个完整的代码来获取索引吗?
  • @Zaid Mustafa 如果您详细解释您的要求会更好。我可以为您提供完整的代码,但问题是我从您的问题中得到的答案是我回答的。像这样解释你有屏幕 A 和屏幕 B 或者你在一个屏幕中有两个 UItableview。
  • 我将添加一个新问题,并附上我想要做什么的屏幕截图。
【解决方案2】:

希望你得到解决方案。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
        {
            if tableView.isEqual(firstTableView) {
               self.updateScecondTableView(index: IndexPath.row) //make a function to retrieve selected index from first table view
            } else {
                // acode
            }
        }

func updateScecondTableView(index:NSIndexPath)  {
        switch index.row {
        case 0:
            // add a code
            break

        default:
            // add a code
            break
        }
    }

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多