Swift TableView in a UIView not displaying data

2019-02-17 07:29发布

I want to create a page which should have a map on the top and 5 row tableview at the bottom. So i created UIViewController put my Map View then put TableView. I created myViewController.swift and myTableViewCell.swift

But when i try on simulator no data showing on 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
    }

}

4条回答
闹够了就滚
2楼-- · 2019-02-17 08:24

Set the dataSource of the tableview.

tableview.dataSource = self tableview.delegate = self

查看更多
仙女界的扛把子
3楼-- · 2019-02-17 08:26

try this:

override func viewDidLoad() {
    super.viewDidLoad()

    // Add this
    tableView.delegate = self
    tableView.dataSource = self
}
查看更多
Bombasti
4楼-- · 2019-02-17 08:27

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.

查看更多
beautiful°
5楼-- · 2019-02-17 08:27

Assign delegate and datasource of UITableView/ UICollectionView through storyboard to reduce code as follows:

Right click on your UITableView, then click on round circle for Delegate/ Datasource and move on to the viewcontroller. For clarity, see the image below.

Click here to view screenshot

查看更多
登录 后发表回答