2015-08-21 57 views
0

我的tableview的应用程序是这样的:Swift |为什么我的UITableView是空的(自定义单元格)

Empty tableview

我不知道为什么label1不工作。

以下是我做到了这一点:

我拖着一个UITableViewController并将其设置为initialViewController。

Main.storyboard

我设置的UITableViewCell风格Custom

UITableViewCell style

然后,我设置的UITableViewController的​​

UITableViewController class

和定义UITableViewCell的

UITableViewCell class

然后,我增加了一些限制:

UITableViewCell label's constraints

这里的TableViewController.swift

import UIKit 

class TableViewController: UITableViewController { 

    let arr = ["abc", "def", "ghi", "jkl", "mno"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 // This should more than one if you want to see the sections and rows in your tableview 
    } 

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("item", forIndexPath: indexPath) as! TableViewCell 
     cell.label1.text = arr[indexPath.row] 
     return cell 
    } 
} 

而这里的TableViewCell.swift

import UIKit 

class TableViewCell: UITableViewCell { 
    @IBOutlet weak var label1: UILabel! 
} 

任何人都知道如何解决这个问题?我真的需要使用自定义单元格!

P.S.我使用的Xcode 7测试版5,雨燕2.0

+0

你加约束表视图? – anhtu

+0

我使用'UITableViewController',@anhtu,所以它会自动为我添加。 – David

+1

'override func numberOfSectionsInTableView(tableView:UITableView) - > Int { return 0 }'出错了 – anhtu

回答

4

试试这个

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
+0

非常感谢 – David