2014-09-04 59 views
2

我一直在努力弄清楚這是如何工作的,但我似乎無法得到它。我曾嘗試過使用其他教程,但隨着許多測試版本的發佈,一切都在不斷變化。我對IOS開發相當陌生,所以我很掙扎。如何使用Swift將項目添加到UITableView?

故事板中我有UITableView,它包含一個標識符爲「myCell」的單元。

這是我到目前爲止。當我運行IOS模擬器時,表格視圖上沒有任何提示。

有關如何解決此問題的任何建議?

class ViewController: UITableViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell 
    cell.textLabel?.text = "Cell #: \(indexPath.row)" // display the row number 
    return cell 
    } 
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10; // testing out with 10 cells 
    } 
} 
+0

您運行的是哪個版本的Xcode?我無法按原樣編譯。 – Mike 2014-09-04 21:10:19

+0

新版本發佈。 Xcode 6 Beta 7. – xNightxOwl 2014-09-04 21:14:21

回答

4

添加功能

optional func numberOfSectionsInTableView(tableView: UITableView) -> Int 

,並返回你想要的部分的數量。

你應該確保在故事板您的UITableViewController擁有一流的ViewController像這樣:

enter image description here

和視圖控制器既是像這樣(引用奧特萊斯)的的UITableViewController的委託和數據源:

enter image description here

你還應該檢查你的UITableViewController是否設置爲initialViewController,如果你沒有看到任何li根本不需要(檢查底部的那個)。

enter image description here

+0

哇,非常感謝,夥計!我將自定義類從「UITableViewController」更改爲「ViewController」,它工作。你能解釋爲什麼嗎? – xNightxOwl 2014-09-04 21:10:37

+0

我不是專家,但這裏是我的解釋:iOS將加載UITableViewController與類UITableViewController,它將只顯示一個空的UITableViewController。 ViewController是UITableViewController的一個子類,基本上爲UITableViewController類添加了功能。在故事板中,我們需要清楚地說明UITableViewController是ViewController類,因此它可以獲得您添加的所有額外功能。 – jamespick 2014-09-04 21:15:28