UITableView原型单元格未找到

问题描述:

每一个。 大量的搜索和不同的测试后我的代码还没有工作...UITableView原型单元格未找到

我尝试做用“dequeueReusableCellWithIdentifier”一个简单的原型细胞白衣一个简单的UITableView,但此功能不会发现我的原型细胞。

我的代码:

import Foundation 
import UIKit 

class DanceClubViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var tableViewSound: UITableView! 

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    self.tableViewSound.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellVote") 
    self.tableViewSound.dataSource = self 
    self.tableViewSound.delegate = self 
    self.tableViewSound.reloadData() 
} 

override func viewWillAppear(animated: Bool) { 

    super.viewWillAppear(animated) 
}  

@IBAction func backToViewPlay(sender: AnyObject) { 

    self.navigationController?.popToRootViewControllerAnimated(true)   
} 

func tableView(tableView:UITableView, numberOfRowsInSection section:Int)->Int 
{ 
    return 10 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell : UITableViewCell = self.tableViewSound.dequeueReusableCellWithIdentifier("cellVote") as UITableViewCell 

    return cell 
} 

如果有人已经有这个问题?

我的原型细胞的标识是:cellVote

+1

什么是例外? – mustafa 2014-10-12 00:13:39

+0

异常?代码有效,我可以在模拟器上进行调试,代码在func“cellForRowAtIndexPath”中运行良好,但是我的单元格没有考虑到我的'cellVote'方面。当我尝试添加这一行:'var num:UILabel = cell.viewWithTag(1)as UILabel'应用程序崩溃并且说我不能将NIL添加到UILabel中...但是我的UILabel在我的我的故事板上的单元格。并带有好的标签。 – 2014-10-12 04:29:35

解决您的标签是在另一个文件中创建一个自定义TableViewCell类的关键:

import UIKit 

class DanceCell: UITableViewCell { 

    @IBOutlet weak var num: UILabel! //connect the label 

} 

然后为您的ViewController:

import Foundation 
import UIKit 

class DanceClubViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var tableViewSound: UITableView! 


    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     self.tableViewSound.dataSource = self 
     self.tableViewSound.delegate = self 
     self.tableViewSound.reloadData() 
    } 

    override func viewWillAppear(animated: Bool) { 

     super.viewWillAppear(animated) 
    } 

    @IBAction func backToViewPlay(sender: AnyObject) { 

     self.navigationController?.popToRootViewControllerAnimated(true) 
    } 


    func tableView(tableViewSound:UITableView, numberOfRowsInSection section:Int)->Int 
    { 
     return 10 
    } 

    func tableView(tableViewSound: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableViewSound.dequeueReusableCellWithIdentifier("cellVote", forIndexPath: indexPath) as DanceCell 

     cell.num.text = "Hello" 

     return cell 
    } 
} 

我删除了一些东西,并在'cellForRowAtIndexPath'函数中添加了他的关键行来注册该单元格。然后我就可以与标签进行沟通。确保原型单元格已将'cellVote'设置为属性检查器中的标识符。