2017-10-18 122 views
0

我的视图控制器中有一个表视图控制器。当我给它静态行数和单元格的行索引方法名称时,它在表格视图中对我来说没有任何显示。我也重装表视图,但它没有显示,我不知道为什么会这样,IOS中的表视图中的数据加载问题

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell (style: .subtitle, reuseIdentifier: "cellID") 
    let message = messages[indexPath.row] 
    cell.textLabel?.text = message.text 

    return cell 
} 
+1

你有没有设置'代表及datasource'你的tableview –

+0

你需要设置代表。 –

回答

0

复核如果u已设置

tableView.delegate = self 
tableView.datasource = self 

也把一个断点cellForRowAtIndexpath检查如果代码运行通过该块。

也重新检查cellIdentifier(cellID)是否正确。

+0

非常感谢。你能指导我一个简短的问题吗? @DHEERAJ – Raheel

+0

当然,这是什么? – DHEERAJ

+0

我已经在我的项目中使用过桥,我在NSUserDefault中存储了字符串的值,现在我想将它传递给我的swift类,但是当我通过它时不显示值。 @DHEERAJ – Raheel

0
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate { 

       //Getting the reference variables of storyboard 
       @IBOutlet weak var tableView:UITableView! 
       var messages = [String]() 

       override func viewDidLoad() { 
        super.viewDidLoad() 


        //Add some packages 
        messages.append("Super") 
        messages.append("Free ") 
        messages.append("Miscellaneous") 
        messages.append("All ") 

        tableView.datasource = self 
        tableView.delegate = self 


       } 

    func numberOfSections(in tableView: UITableView) -> Int { 
      return 1 
     } 

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell (style: .subtitle, reuseIdentifier: "cellID") 
     let message = messages[indexPath.row] 
     cell.textLabel?.text = message.text 

     return cell 
    } 
} 
+0

谢谢,我得到了我的回答 – Raheel

0

使用此。这一次对我的作品

class yourViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 
     @IBOutlet weak var tableView:UITableView! 

     override func viewDidLoad() { 
      super.viewDidLoad() 
      tableView.datasource = self 
      tableView.delegate = self 
     } 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return messages.count 
     } 

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      let cell=tableView.dequeueReusableCell(withIdentifier: "cellID",for : indexPath) 


      let message = messages[indexPath.row] 
      cell.textLabel?.text = message.text 

      return (cell) 

     } 
    } 
+0

谢谢我得到了我的答案 – Raheel