2017-04-26 99 views
1

正如標題所示,我試圖顯示鏈接到控制器的標籤中的一些文本,但無法顯示除空白單元格外的任何內容。每個標籤都正確連接到控制器,但單元格仍然是空的。代碼有問題嗎?TableView顯示空單元格

import UIKit 

class TableViewCell: UITableViewCell{ 

    @IBOutlet weak var label1: UILabel! 
    @IBOutlet weak var label2: UILabel! 
    @IBOutlet weak var Date: UILabel! 
} 
class Controller: UIViewController, UITableViewDataSource,UITableViewDelegate { 

    let textCellIdentifier = "ShowCell" 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tableView.delegate = self 
     self.tableView.dataSource? = self 
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ShowCell") 
     LeagueData.single.addListener { 
      self.tableView.reloadData() 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // return team.count 
     return 7 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "ShowCell") as! TableViewCell! 
     cell?.label1?.text = "sample text" //LeagueData.single.schedule[0].team1.name 
     cell?.label2.text = LeagueData.single.schedule[0].team2.name 
     cell?.Date.text = LeagueData.single.schedule[0].date 
     print(LeagueData.single.schedule[0].date) 
     return cell! 
    } 
} 
+0

分享你的故事板/ XIB文件? –

+0

你在tableview中使用自定義的單元格嗎? –

回答

1
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ShowCell") 

改變這種

tableView.register(TableViewCell.self, forCellReuseIdentifier: "ShowCell") 

,或者如果你使用廈門國際銀行,改變

tableView.registerNib(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "ShowCell") 
+0

...並且如果您正在使用故事板,則根本不要註冊該單元格。 – vadian

+0

我注意到這個問題在將我的工作與Xcode上的另一個分支合併後很常見。有沒有辦法來防止未來的問題? –

相關問題