Ios Swift自定义UITableViewCell标签始终为零
我已经被这个问题困扰了好几天,所以如果有人能帮助我,我会非常高兴。 我正在尝试创建一个动态UITableView,为此我创建了一个自定义UITableView子类,还创建了一个自定义UITableViewCell子类,因为每个单元格中需要几个UILabel和一个UIButton。 单元格已创建,但问题是标签的值始终为零,因此单元格未正确显示。 ,故事板的外观,以及我在运行程序时看到的内容 下面是我的UITableViewCell子类:Ios Swift自定义UITableViewCell标签始终为零,ios,iphone,uitableview,cocoa-touch,swift,Ios,Iphone,Uitableview,Cocoa Touch,Swift,我已经被这个问题困扰了好几天,所以如果有人能帮助我,我会非常高兴。 我正在尝试创建一个动态UITableView,为此我创建了一个自定义UITableView子类,还创建了一个自定义UITableViewCell子类,因为每个单元格中需要几个UILabel和一个UIButton。 单元格已创建,但问题是标签的值始终为零,因此单元格未正确显示。 ,故事板的外观,以及我在运行程序时看到的内容 下面是我的UITableViewCell子类: import UIKit class QuestionTa
import UIKit
class QuestionTableViewCell: UITableViewCell {
@IBOutlet var student: UILabel!
@IBOutlet var labDesk: UILabel!
@IBOutlet var topic: UILabel!
@IBOutlet var answers: UILabel!
}
import UIKit
class QuestionViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var table: UITableView!
struct Question {
var student: String
var labDesk: String
var topic: String
var answered: String
}
override func viewDidLoad() {
super.viewDidLoad()
table.estimatedRowHeight = 50
table.dataSource = self
table.delegate = self
self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as QuestionTableViewCell
cell.student.text = "random string"
cell.labDesk?.text = "25/A"
cell.topic?.text = "string"
cell.answers?.text = "3"
return cell
}
}
以及我的UITableView子类:
import UIKit
class QuestionTableViewCell: UITableViewCell {
@IBOutlet var student: UILabel!
@IBOutlet var labDesk: UILabel!
@IBOutlet var topic: UILabel!
@IBOutlet var answers: UILabel!
}
import UIKit
class QuestionViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var table: UITableView!
struct Question {
var student: String
var labDesk: String
var topic: String
var answered: String
}
override func viewDidLoad() {
super.viewDidLoad()
table.estimatedRowHeight = 50
table.dataSource = self
table.delegate = self
self.table.registerClass(QuestionTableViewCell.self, forCellReuseIdentifier: "cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as QuestionTableViewCell
cell.student.text = "random string"
cell.labDesk?.text = "25/A"
cell.topic?.text = "string"
cell.answers?.text = "3"
return cell
}
}
首先,如果类存在于Interface Builder中,则不必注册该类 其次,您应该
dequeueReusableCellWithIdentifier:forindexath
而不是dequeueReusableCellWithIdentifier
第三,UITableViewController
已经有一个名为tableView
的属性,因此不需要为table
创建IBOutlet,因为UITableViewController已经处理了这个问题。它还符合UITableViewDataSource
和UITableViewDataSource
,因此它们是无关的
第四,不要为table
设置属性,为tableView
设置属性
第五,cell.labDesk.text=”“
就足够了,无需将其设置为可选
如果所有IBOutlet都连接好了,单元标识符设置正确,并且进行了这些修改,那么它就可以工作了
class QuestionTableViewCell: UITableViewCell {
@IBOutlet var student: UILabel!
@IBOutlet var labDesk: UILabel!
@IBOutlet var topic: UILabel!
@IBOutlet var answers: UILabel!
}
class QuestionViewController: UITableViewController {
struct Question {
var student: String
var labDesk: String
var topic: String
var answered: String
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 50
tableView.dataSource = self
tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as QuestionTableViewCell
cell.student.text = "random string"
cell.labDesk.text = "25/A"
cell.topic.text = "string"
cell.answers.text = "3"
return cell
}
}
我可能会迟到,但我刚刚解决了一个类似的问题 确保已在UITableViewCell的InterfaceBuilder中设置标识符
尝试删除
self.table.registerClass(QuestionTableViewCell.self,forCellReuseIdentifier:“cell”)
如果您使用带nib的单元格,请确保您使用Registernb:forCellReuseIdentifier:
在表视图中注册该单元格。如果单元格只有一个类,那么对于那些在尝试了所有可能的解决方案后仍在尝试解决此问题的人,请使用注册表类:forCellReuseIdentifier::
断开/重新连接故事板中的IBOutlet应该可以做到这一点 如果您使用带有Xib的表格单元格。您需要将手机注册到
register(_:forCellReuseIdentifier:)
最重要的部分是将包含自定义单元格的xib注册到表视图中。因此,在viewDidLoad()方法中添加以下代码
如果尚未为标签添加约束,则即使创建了自定义单元格,也不会创建约束。 确保添加了一些约束。不要忘记添加:
tableView?.register(UINib(nibName: "xyz",
bundle: nil),
forCellReuseIdentifier: "abc")
确保所选单元格位于右侧的“
模块”
,如有必要,请继承:
如果没有,您的
IBOutlets
将为零。尝试使用dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)并确保您的单元格id匹配,如果该单元格不是可选的,您不应该使用cell.labDesk?.text,只使用cell.labDesk.text。我已经尝试过了,它给出了我的例外:2015-03-13 15:31:09.626 LABhelper[40046:5574601]***在-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]、/SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:6116这是我的函数在您建议的情况下的断言失败:var cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为QuestionTableViewCell,我没有将它们设置为可选的,但不知何故它们变成了可选的,因此没有?生成失败。我已经告诉过您的情况是,如果它不是可选的,则不会运行。cell.student.text=“random string”致命错误:在展开可选值时意外发现nil。它是否未编译或在打开时立即崩溃?它在我到达UITableViews时立即崩溃。因为此代码正常工作,所以可能是您的IBOutlets未正确连接。啊,您使用的视图控制器中包含表视图,请注意,这不是UITableViewCocontroller如果您使用的是nib,您需要的确切语法是:tableView.registerNib(UINib(nibName:“YOURNIBNAME”,bundle:nil),forCellReuseIdentifier:“YOURREUSEID”)
这使Trickit工作正常。但是为什么我们要删除这一行bcz它是注册customcell rite吗?@iApple,因为该类已经存在于界面生成器中。我删除了这一行,我的工作正常。有点困惑为什么我不需要它,我猜在界面生成器中设置自定义类也有相同的功能注册自定义单元格类?这是正确的,并帮助我在以下URL找到了解决问题的方法…我的nib/自定义单元格注册不正确哇,太有用了!这也是我的问题。在我的情况下,它工作得很好,直到我滚动我的CollectionView,一个简单的标签列表。只要我将新行滚动到vi中ew它以零值崩溃。谢谢!欢迎使用StackOverflow上的@NewBe,如果您能附加一些代码片段,它将真正帮助其他人。有关如何编写好答案的更多详细信息,请访问