2017-10-18 117 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