2014-09-04 77 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