用Swift填充表视图

13

嗨,我想填充这个表格视图。 视图中的配置

我没有使用静态表视图,因为出现错误,我决定使用动态表视图并禁用表格中的滚动。 该表格视图仅用于信息展示:

  @IBOutlet var tableInfoQuake: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = quake.place


    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = quake.place
    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = "Mag: \(quake.mag)"
    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Depth: \(quake.depth)"

但在表格视图中我没有看到任何东西。

我该怎么做?谢谢。

编辑:我这样做来修改每一行:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
    var cellIdentifier = "quakeInfo"
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
    if indexPath == NSIndexPath(forRow: 0, inSection: 0){
        cell.textLabel!.text = quake.place
        cell.detailTextLabel!.text = "Mag: \(quake.mag)"
    }
    if indexPath == NSIndexPath(forRow: 1, inSection: 0){
      cell.textLabel!.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
        cell.detailTextLabel!.text = "Depth: \(quake.depth)"
    }

    return cell

}
2个回答

40

哦,TableViews...

所以,你想要动态地填充一个tableView。好的...这里是我的朋友:


第一步:实现DataSource

好的...那么你知道什么是协议吗?如果不知道...看这个视频吧(不是我演讲的)。DataSource是UITableView将调用以检索显示所需数据的内容。具体来说,这里提到的DataSource是UITableViewDataSource。

所以...

    // change top to...

    class MyViewController:UIViewController, UITableViewDataSource

这使得 UITableViewDataSource 成为您的 ViewController 的必备条件。接下来,您需要在 UITableView 上设置数据源。所以...

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = quake.place

        tableInfoQuake.datasource = self     
    }

假设你现在想要向表格中添加7个单元格,并且这7个单元格都要写上“Hello Man”。为了说明,我将使用最糟糕但最简单的方法。如果您希望我深入解释,请在下面留言,我会使用更好的方法。

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 7
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
        let cell = UITableViewCell()
        let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
        label.text = "Hello Man"
        cell.addSubview(label)
        return cell
    }

第一步就讲到这里。我们已经告诉表格视图控制器需要哪些数据,同时返回了可供使用的数据。


第二步:实现代理

和第一步类似,这一步也是从顶部添加一些内容开始的……

    // change top to...

    class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate

然后,与顶部类似,我们将编辑viewDidLoad()函数...

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = quake.place

        tableInfoQuake.datasource = self
        tableInfoQuake.delegate = self
    }

现在我们必须实现委托方法,指定每个单元格的高度。

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }

我们现在已经将委托添加到表格中,并实现了告诉UITableView每个单元格高度的方法。


让我们把所有东西都放在一起

    class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate {

        @IBOutlet var tableInfoQuake: UITableView!

        override func viewDidLoad() {
            super.viewDidLoad()

            tableInfoQuake.datasource = self
            tableInfoQuake.delegate = self
        }


        // UITableViewDataSource Functions

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 7
        }

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
            let cell = UITableViewCell()
            let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
            label.text = "Hello Man"
            cell.addSubview(label)
            return cell
        }


        // UITableViewDelegate Functions

        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            return 50
        }        
    }

祝你好运。

ZR


好的,但是我该如何选择单独的一行并对其进行操作呢?因为两行是不同的。当我实现 func cellForRowAtIndexPath 时,我为每个单元格都实现了相同的结构,而实际上我想要每一行都有一个不同的结构。像这样: tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = quake.place 以及这样: tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "坐标:((quake.longitude),(quake.latitude))" - Andrea485
在cellForRowAtIndexPath方法中,您会得到indexPath参数,该参数告诉您表格正在请求哪个单元格。要处理特定的单元格,例如第二个单元格,您可以这样做:if(indexPath.row == 1) { //设置代码 } - xytor
2
非常非常有帮助。只需注意您可能需要将 datasource 更改为 dataSource 的大小写。 - Nebril
在创建UILabel时,对于Swift 3,请使用frame: CGRect(x:0, y:0, width:200, height:50) - Christopher Klaus

2

故事板中的单元格是原型单元格。这意味着它们实际上不在表视图中,但可以用作模板。

为了填充表视图,您需要:

  1. 在故事板中,通过从表视图的连接菜单中拖动“数据源”连接到您的视图控制器,将您的视图控制器设置为表视图的数据源。
  2. 使您的视图控制器符合UITableViewDataSource协议。
  3. 在您的视图控制器中实现以下方法:
    • func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    • func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

使用这种方式,您如何引用tableview以执行刷新数据?仍然需要IBOutlet吗? - Adrian E. Labastida Cañizares
是的,您需要 IBOutlet 来引用表格视图。 - xytor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接