所以我正在尝试实现一个简单的 UITableView
,当我编译并运行我的代码时,它会显示一个空表。这是我的代码:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
var items:[String] = ["foo", "ban", "bar"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.frame = self.view.bounds
self.view!.addSubview(tableView)
}
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
var theItem:String = items[indexPath.row]
cell.textLabel.text = theItem
return cell
}
}
为什么表中没有显示项目?
最佳答案
好吧,你没有设置 tableViews 委托(delegate)和数据源,尝试这样做并确保你的 tableview 委托(delegate)/数据源方法被调用......
tableView.delegate = self
tableView.dataSource = self
关于ios - TableView 未填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25085917/