2015-08-21 59 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