2015-09-17 147 views
4

我想創建一個頁面,它應該在頂部有一個映射,在底部應該有一個5行的tableview。所以我創建了UIViewController把我的地圖視圖,然後把TableView。我創建myViewController.swift和myTableViewCell.swiftUIView中的Swift TableView不顯示數據

但是,當我嘗試在模擬器上沒有數據顯示在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 
    } 

} 

回答

11

試試這個:

override func viewDidLoad() { 
    super.viewDidLoad() 

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

我真的已經很努力解決這個問題,但解決的辦法是那麼容易嗎?:)非常感謝你 – fuzunspm

+0

不要忘記辦始終做到這一點,也與的CollectionView,... – Masterfego

+0

這是什麼兩行真的嗎?在此先感謝 –

4

你應該做的二分之一的方式:在viewDidLoad中

tableView.delegate = self 
tableView.dataSource = self 

()您的viewController 或 的 - 選擇的tableView並通過按控制拖到viewController,首先選擇數據源,然後再次選擇委託。

-1

設置tableview的dataSource。

tableview.dataSource =自 tableview.delegate =自

+1

覆蓋FUNC viewDidLoad中(){ super.viewDidLoad() //添加這個 tableView.delegate =自 tableView.dataSource =自 } –

1

分配代表,並通過故事板的UITableView/UICollectionView的數據源,以減少代碼如下:

鼠標右鍵點擊你的UITableView,然後單擊圓頭圓代表/數據源並轉到viewcontroller。爲了清楚起見,請參閱下圖。

Click here to view screenshot