2016-12-15 40 views
-2

我是iOS开发新手,基本上我试图用数组中的字符串值填充TableView。iOS Swift - TableView行为空

但是,当我运行应用程序时,空白行显示,并没有显示文本值。我有正确的编码吗?

import UIKit 

    class SelectIssueController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

     @IBOutlet var issuesTableView: UITableView! 

     var issues = [Issue]() 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      self.issues = ["Test1", "Test2", "Test3"] 

     self.issuesTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") 

     } 

     override var preferredStatusBarStyle: UIStatusBarStyle{ 
      return UIStatusBarStyle.lightContent 
     } 

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return self.issues.count 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

      let cell = self.issuesTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell 

      cell.textLabel?.text = issues[indexPath.row] 

      //Even if I manually set a value, the rows are still blank 
      //cell.textLabel?.text = "Hello World" 

      return cell 

     } 
    } 
+2

你设置你的SelectIssueController实例是UITableView的数据来源? –

+0

您是否在谈论参考插座? @LucasDerraugh如果是这样,是的,我有:) – Dinuka

+2

tableView的dataSource在故事板或问题TableView.datasource = self –

回答

4

您可以通过两种方式设置表视图数据源和委托。 1.点击Cntrl+dragtableViewview controller。参见下面的图

enter image description here

  • 创建tableViewoutlet并分配其DataSource和代表在ViewDidLoad
  • 在你的榜样,你已经有一个出口issuesTableView,所以你可以这样写:

    issuesTableView.dataSource = self 
    issuesTableView.delegate = self 
    

    谢谢:)

    +0

    愚蠢的我!这固定的问题哈哈:)我会在5分钟内接受它 – Dinuka

    +0

    快乐编码:) @Dinuka –

    +0

    我冒昧地更新你的答案有点@Karthick,希望你不介意(使用'issuesTableView'而不是'myTableView '建议2) – pbodsk