如何通过表视图传递数据

时间:2016-07-21 08:09:58

标签: swift uitableview

我的数据结构示例..

  Sports  
      Bat-and-ball
           Baseball
           Softball
           Cricket
      Hockey
           Field Hockey
           Ice Hockey
           Roller Hockey
  Engineering  
      Computer Science
           Software Engineer
           Electrical Engineer

这里我有三个级别的数据,你可以看到。但默认情况下,在tableview中它只提供最多2级。我该如何显示第三级数据。 我只能使用customcell在行中显示第一级数据和在行中显示第二级数据。现在在哪里显示第三级数据。所以我可以根据行数进行迭代..

我的屏幕应该是那样..

enter image description here

3 个答案:

答案 0 :(得分:2)

有很多方法可以做到这一点。

我稍后会列出其中几个。 但首先,您可能会考虑在一个表视图中显示3个级别的数据是否是一个良好的用户体验。用户可能很难浏览它。

所以这些是一些选择。

  1. 在选择器(下拉列表)中显示顶级数据(让我们称之为类别),然后在表视图中,只有两个级别(部分和行)用于所选类别。 以下是此类下拉视图的示例: https://github.com/PhamBaTho/BTNavigationDropdownMenu

  2. 如果您确实需要显示包含3个级别数据的列表,您可以拥有一个表格视图,该视图将顶级数据显示为一个部分,第二个级别显示为一行。对于每一行,相应的单元格将包含在一个表视图中(或者可能只是一个堆栈视图),只显示第三级的行。

答案 1 :(得分:0)

您可以在自定义单元格中添加表格,第二级和第三级数据可以显示在该内部表格上。

答案 2 :(得分:0)

  

此代码在我的设备上运行得非常漂亮。

<强>故事板 enter image description here

<强> TableViewController1

import UIKit

class TableViewController1: UITableViewController {

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return group1.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = group1[indexPath.row].title

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "segue1" {

        let indexPaths = self.tableView!.indexPathsForSelectedRows!
        let indexPath = indexPaths[0] as NSIndexPath

        let vc = segue.destinationViewController as! TableViewController2
        vc.currentGroup = group1[indexPath.row].title

    }
}
}

<强> TableViewController2

import UIKit

class TableViewController2: UITableViewController {

var currentGroup: String?
var filteredData = [Data]()

override func viewDidLoad() {
    super.viewDidLoad()

    for data in group2 {

        if data.group == currentGroup {
            filteredData.append(data)
        }

    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredData.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = filteredData[indexPath.row].title

    return cell
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "segue2" {

        let indexPaths = self.tableView!.indexPathsForSelectedRows!
        let indexPath = indexPaths[0] as NSIndexPath

        let vc = segue.destinationViewController as! TableViewController3
        vc.currentGroup = group2[indexPath.row].title

    }
}
}

<强> TableViewController3

import UIKit

class TableViewController3: UITableViewController {

var currentGroup: String?
var filteredData = [Data]()

override func viewDidLoad() {
    super.viewDidLoad()

    for data in group3 {

        if data.group == currentGroup {
            filteredData.append(data)
        }

    }
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredData.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = filteredData[indexPath.row].title

    return cell
}

}

数据类

import Foundation

class Data {

var title: String
var group: String?

init (title: String, group: String?) {
    self.title = title
    self.group = group
}
}

let group1 = [Data(title: "Sport", group: nil), Data(title: "Engineering", group: nil)]
let group2 = [Data(title: "Hockey", group: "Sport"), Data(title: "CS", group: "Engineering")]
let group3 = [Data(title: "Ice Hockey", group: "Hockey"), Data(title: "Software Engineer", group: "CS")]

P.S。我讨厌让事情变得复杂。

快乐的黑客攻击!