ios - 快速填充 TableView

标签 ios uitableview swift

嗨,我想填充这个表格 View configuration in the view

我不使用静态 TableView ,因为我出错了,我决定使用动态 TableView 并禁用表中的滚动。 表格 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)"

但在表格 View 中我什么都看不到。 我能怎么做?谢谢。

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

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 。好吧……给你,我的 friend :


第 1 步:实现数据源

好的....那么你知道协议(protocol)是什么吗?好吧,如果你不... BAM, now you do (视频中不是我)。 DataSource 是 UITableView 将调用以检索它需要显示的数据。具体来说,所讨论的数据源是 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 步:实现委托(delegate)

很像第 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
    }

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

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

我们现在已经将委托(delegate)添加到表中,并实现了告诉 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

关于ios - 快速填充 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26309205/

相关文章:

ios - Realm 迁移 : Why in documentation it is recommended to call the migration block each time without checking the schema version before?

ios - UITableViewCell 重新加载后 MKMapView 变白

swift - 无法将 firebase 导入到我的 swift 项目中

ios - 在 iOS 中没有 userControl 的情况下在 Webview 中播放 Youtube Url

ios - 如何从警报中的 TextField 获取输入文本

ios - 在 UIScrollview 中使用 ChildViewControllers?

ios - 无法根据其内容设置 UITextView 高度

ios - 如何垂直居中WebKitView

json - 如何从 JSON 获取这些 url 并将其加载到我的 Xcode UICollection 中?

android - 自动连接已配对的 BT 设备