在swift中填充表视图

时间:2014-10-10 22:57:46

标签: ios uitableview swift

您好我想填充此表格视图 configuration in the view

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

  @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 个答案:

答案 0 :(得分:36)

哦TableViews ......

因此,您希望动态填充tableview。嗯......在这里,你去找我的朋友:


第1步:实施DataSource

好的....所以你知道协议是什么?好吧,如果你不...... BAM, now you do(视频中不是我)。 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
    }

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


第2步:实施代表

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

    // 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

答案 1 :(得分:2)

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

要填充tableview,您需要:

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