在swift中填充表视图 [英] Populate table view in swift

查看:97
本文介绍了在swift中填充表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想填充此表格视图

Hi i want to populate this tableview

我不使用静态表视图,因为我收到错误,我决定使用动态表视图并禁用表中的滚动。
表格视图仅用于以下信息:

i don't use the static table view because i take an error, and i decide to use the dynamic table view and disable the scroll in the table. The table view is use only for the information :

  @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)"

但在表格视图中我没有任何可视化。
我该怎么办?谢谢。

but in the table view i don't visualize nothing. how can i do? thank you.

编辑:我这样做是为了修改每一行:

i do this to modify each row :

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

}


推荐答案

哦TableViews ...



所以,您想要动态填充tableview。嗯......在这里你去找我的朋友:

Oh TableViews...

So, you want to dynamically populate a tableview. Well... here you go my friend:

好的....所以你知道协议是什么?好吧,如果你不... BAM,现在你做了(不是我的视频)。 DataSource是UITableView将调用以检索它需要显示的数据的内容。具体来说,有问题的DataSource是UITableViewDataSource。

Okay.... so you know what a protocol is? Well if you don't ... BAM, now you do (Not me in video). A DataSource is what the UITableView will call to retrieve the data it requires to display. To be specific, the DataSource in question is UITableViewDataSource.

so ...

    // change top to...

    class MyViewController:UIViewController, UITableViewDataSource

这使得UITableViewDataSource成为ViewController的一项要求。接下来,您需要在UITableView上设置数据源。所以....

This makes UITableViewDataSource a requirement for your ViewController. Next you need to set the datasource on your UITableView. So....

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

        tableInfoQuake.datasource = self     
    }

假设您想现在向表中添加7个单元格,并且所有7个单元格都会显示Hello Man。对于这个例子,我将以最糟糕但最简单的方式做到这一点。如果你想让我更深入,只需在下面评论,我会做得更好。

Lets say you want to now add 7 cells to the table, and all 7 will say "Hello Man". For this example, I am going to do it the worst yet simplest way. if you want me to go more in depth, just comment below, and I will do it the better way.

    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
    }

这就是第一步。我们已经告诉桌子,ViewController在数据方面有它正在寻找的东西,我们已经返回了数据供它使用。

So thats it for step one. We have told the table that the ViewController has what it is looking for in terms of data, and we have returned data for it to use.

与第1步非常相似,此步骤首先在顶部添加内容...

Much like Step 1, this step starts with adding something to the top...

    // change top to...

    class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate

然后,再次,就像顶部一样,我们将编辑 viewDidLoad()函数......

Then, again, much like the top, we will edit the viewDidLoad() function...

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

        tableInfoQuake.datasource = self
        tableInfoQuake.delegate = self
    }

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

And now we must implement the delegate methods that designate the height of each cell.

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

我们现在已将代表添加到表,并实现了告诉UITableView每个单元格的高度是什么的方法。

We have now added the delegate to the table, and implemented the method that tells the UITableView what the height of each cell is.

    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

这篇关于在swift中填充表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆