Dan*_*ith 0 xcode uitableview ios swift
我正在尝试UITableView在Swift 中创建一个。我遵循了一个教程,但是table没有我尝试将其放入代码中的值。这是代码:
class settingsVC2: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
}
}
Run Code Online (Sandbox Code Playgroud)
您必须将表(委托和数据源)连接到 ViewController 本身。
或者您可以通过将以下几行添加到您的viewDidLoad():
self.tableView.dataSource = self
self.tableView.delegate = self
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
760 次 |
| 最近记录: |