我正在尝试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)!")
}
}
您必须将表(代理和数据源)连接到ViewController本身。
或者,您可以通过在您的代码中添加以下几行来以编程方式执行此操作viewDidLoad()
:
self.tableView.dataSource = self
self.tableView.delegate = self
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句