【问题标题】:How to open up a new tableview programmatically in swift如何在swift中以编程方式打开一个新的tableview
【发布时间】:2016-08-05 17:53:44
【问题描述】:

我在情节提要中创建了一个包含 11 行的 tableview。我希望能够点击每一行并将其打开到它自己的 tableview 控制器中,我将在其中存储数据,具体取决于所点击的行。

      var array = ["row 1", "row 2", "row 3", "row 4", "row 5", "row 6", "row 7", "row 8", "row 9", "row 10", "row 11"]

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("prototypeCellIdentifier", forIndexPath: indexPath)
        cell.textLabel?.text = array[indexPath.item]
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 
    return cell
}

这是我为创建行所做的。

【问题讨论】:

  • UITableViewDelegate 方法func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
  • 学习阅读 Apple 文档。这一切都在里面。或搜索互联网。互联网上有无数关于此的教程和示例。
  • @dasdom 我想你可能正在做点什么。如果每个人都这样做,并且堆栈溢出不必存在。

标签: ios swift uitableview


【解决方案1】:

简而言之:

  1. 转到您的故事板
  2. 向 Storyboard 添加另一个 TableViewController
  3. 在11行的TableView的tableViewCell上按住鼠标右键,将其拖向新创建的TableViewController
  4. 松开鼠标,将出现一个弹出窗口。
  5. 选择其中一项,例如show
  6. 构建并运行您的应用,单击一个单元格,一个新的 TableViewController 将打开

要将数据发送到通过单击一行显示的 ViewController,您应该查看方法 prepareForSegue(_)

【讨论】:

  • 这里也有很多关于如何在视图控制器之间传递数据的好答案。也许你可以链接其中一个?
  • @Losiowaty,好点 :) 有关使用 segues 时发送数据的更多详细信息:stackoverflow.com/a/26089243/5489170 我还找到了有关单击 UITableViewCell 时如何显示新 ViewController 的更详细说明: stackoverflow.com/q/28323925/5489170
猜你喜欢
  • 2021-01-05
  • 2019-09-12
  • 2010-10-29
  • 2012-02-19
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
相关资源
最近更新 更多