我试图做一个tableView(在一个viewController内)显示一组数据(我之前做过的),但这次它不显示数据。我已经将viewController声明为tableView的dataSource和Delegate。这是我的viewControllerClass:UITableView不显示数据,但已经是数据源和委托
class MenuViewController: UIViewController , UITableViewDataSource , UITableViewDelegate {
var menuItems = [ "1" , "2" , "3"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return menuItems.count
}
open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = menuItems[indexPath.row]
return cell
}
}
你在哪里添加视图控制器作为数据源和委托?表视图添加到视图的位置以及它的约束是什么?如果你在'numberOfRowsInSection'和'cellForRowAt'中设置断点,那么这两个断点都会被命中? – keithbhunter
有两件事...确保dataSource和委托在界面构建器中连接或手动设置它们在您的viewDidLoad ...尝试强制tableView.reloadData() – GregP
@keithbhunter我将视图控制器设置为数据源和委托在主.storyboard和我设置了断点,并且都被击中 –