简体   繁体   English

UIView中的Swift TableView不显示数据

[英]Swift TableView in a UIView not displaying data

I want to create a page which should have a map on the top and 5 row tableview at the bottom. 我想创建一个页面,该页面的顶部应具有地图,底部应具有5行tableview。 So i created UIViewController put my Map View then put TableView. 所以我创建了UIViewController,将我的Map View放在TableView中。 I created myViewController.swift and myTableViewCell.swift 我创建了myViewController.swift和myTableViewCell.swift

But when i try on simulator no data showing on tableview. 但是当我尝试模拟器时,tableview上没有显示任何数据。 Only empty cells.I received no error 只有空单元格我没有收到错误

Here is my code. 这是我的代码。 Thank you 谢谢

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
    }

}

try this: 尝试这个:

override func viewDidLoad() {
    super.viewDidLoad()

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

You should do one out of two way : 您应该采用两种方式之一:

tableView.delegate = self
tableView.dataSource = self

in the viewDidLoad() of your viewController OR - select the tableView and dragging by pressing control to viewController, first select the datasource and again doing so select the delegate. 在viewController的viewDidLoad()中,或者-选择tableView并通过按下控件拖动到viewController,首先选择数据源,然后再次选择委托。

Assign delegate and datasource of UITableView/ UICollectionView through storyboard to reduce code as follows: 通过情节提要分配UITableView / UICollectionView的委托和数据源以减少代码,如下所示:

Right click on your UITableView, then click on round circle for Delegate/ Datasource and move on to the viewcontroller. 右键单击您的UITableView,然后单击圆上的Delegate / Datasource,然后转到视图控制器。 For clarity, see the image below. 为了清楚起见,请参见下图。

点击此处查看屏幕截图

Set the dataSource of the tableview. 设置表视图的数据源。

tableview.dataSource = self tableview.delegate = self tableview.dataSource =自我tableview.delegate =自我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM