在UIView中的Swift TableView不显示数据

我想创build一个页面,顶部应该有一个地图,底部是5行的tableview。 所以我创build了UIViewController把我的地图视图,然后把TableView。 我创build了myViewController.swift和myTableViewCell.swift

但是,当我尝试在模拟器上没有数据显示在桌面上。 只有空细胞。我没有收到任何错误

这是我的代码。 谢谢

import UIKit import MapKit class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var mapView: MKMapView! var labels = ["some arrays"] var images = ["some image names in an array"] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return labels.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("mySegue", forIndexPath: indexPath) as! myTableViewCell cell.myCellLabel.text = labels[indexPath.row] cell.myCellImage.image = UIImage(named: images[indexPath.row]) return cell } } 

尝试这个:

 override func viewDidLoad() { super.viewDidLoad() // Add this tableView.delegate = self tableView.dataSource = self } 

你应该从两个方面做一个:

 tableView.delegate = self tableView.dataSource = self 

在你的viewController的viewDidLoad()或者 – selecttableView并通过按下拖动viewController,首先select数据源,然后再次select委托。

通过故事板指定UITableView / UICollectionView的委托和数据源以减less代码,如下所示:

右键单击你的UITableView,然后点击Delegate / Datasource的圆圈,然后进入viewcontroller。 为了清楚起见,请看下面的图片。

点击这里查看截图

设置tableview的dataSource。

tableview.dataSource = self tableview.delegate = self