将数据/行添加到 ViewController 内的 UITableView。迅速

Seb*_*bbe 0 xcode uitableview ios swift

如何在 UIViewController 中填充 UITableView?UITableView 是动态的并使用原型单元格。

(使用斯威夫特)

这就是我所拥有的

MSh*_*hah 5

执行以下操作:

import UIKit

class tableViewOne: UIViewController{

    @IBOutlet var tableView: UITableView!
    // your data
    var dummyData = ["data 0","data 1","data 2"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

extension tableViewOne: UITableViewDataSource, UITableViewDelegate {
    // Define no of rows in your tableView
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dummyData.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        // Default cell
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell

        cell.textLabel!.text = dummyData[indexPath.row]

        return cell;
    }

}
Run Code Online (Sandbox Code Playgroud)

注意不要忘记DataSource在. 如果已经在.DelegateUITableViewstoryboardstoryboard