2016-02-28 94 views
1

我有一個UITableViewController與自定義單元格視圖。我創建了一個空的Interface Builder文檔,然後添加了一個Table View Cell,然後爲它添加了一個標籤。表格視圖單元具有延伸UITableViewCell的相應類。表視圖Cell的在Interface Builder標籤鏈接(outet)的變種在我的自定義類自定義UITableViewCell不出現

class MyTableViewCell: UITableViewCell { 
    @IBOutlet var someLabel: UILabel! 

的問題是,定製電池從來沒有呈現,它總是空白(我試過的背景顏色伎倆) 。我從來沒有看到這個標籤。事實上,標籤始終爲空。

在我UITableViewControllerviewDidLoad(),我已經試過

let nib = UINib(nibName: "MyTableCellView", bundle: nil) 
tableView.registerNib(nib, forCellReuseIdentifier: "myCell") 

以及

tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "myCell") 

我也有

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell 
    print("cellForRowAtIndexPath, cell = \(cell), someLabel = \(cell.someLabel)") 
    return cell 
} 

在運行時,它被作爲出列是cell非零,但cell.someLabel爲零。

需要一個自定義表格視圖單元格渲染?

回答

0

someLabel沒有任何價值。試試:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell 
    cell.someLabel.text = "Put label text here" 
    return cell 
} 
0

我通常這樣做。我在自定義表格視圖單元類中加載xib文件。

class MyTableViewCell: UITableViewCell { 

    @IBOutlet weak var label: UILabel! 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     xibSetup() 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     xibSetup() 
    } 

    func xibSetup() { 
     cell = loadViewFromNib() 
     cell.frame = self.bounds 
     cell.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] 
     addSubview(cell) 
    } 

    func loadViewFromNib() -> UITableViewCell { 
     let bundle = NSBundle(forClass: self.dynamicType) 
     let nib = UINib(nibName: "MyTableViewCell", bundle: bundle) 
     let cell = nib.instantiateWithOwner(self, options: nil)[0] as! UITableViewCell 
     return cell 
    } 

} 

隨着:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyTableViewCell 
    print("cellForRowAtIndexPath, cell = \(cell), someLabel = \(cell.someLabel)") 
    return cell 
} 

一件事做的是設置文件的所有者在MyTableViewCell.xib文件MyTableViewCell類。