UIView中的Swift TableView不显示数据

时间:2015-09-17 10:28:38

标签: ios swift uitableview uiview uiviewcontroller

我想创建一个页面,其顶部应有地图,底部有5行tableview。所以我创建了UIViewController,然后把我的Map View放到TableView中。我创建了myViewController.swift和myTableViewCell.swift

但是当我尝试使用模拟器时,没有数据显示在tableview上。只有空单元格。我没有收到错误

这是我的代码。谢谢

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
    }

}

3 个答案:

答案 0 :(得分:11)

试试这个:

override func viewDidLoad() {
    super.viewDidLoad()

    // Add this
    tableView.delegate = self
    tableView.dataSource = self
}

答案 1 :(得分:6)

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

tableView.delegate = self
tableView.dataSource = self
在viewController的viewDidLoad()中 要么   - 选择tableView并通过按control到viewController进行拖动,首先选择数据源并再次选择该委托。

答案 2 :(得分:0)

设置tableview的dataSource。

tableview.dataSource = self tableview.delegate = self