【发布时间】:2016-03-16 05:19:33
【问题描述】:
尝试在我的应用中处理几个场景,但我无法让 tableViews 正常工作。
我正在执行 tableView 之类的设置,用户可以在 两种可能的设置之间进行选择。每个选项都会导致新的 tableView,用户必须在其中选择一个可用选项。一旦用户选择了一个,第一个场景中的标签将显示选择的值(也存储为 NSUserDefaults)。因此,如果我没记错的话,这可以通过 3 个静态 tableViews 来实现。以下代码是我尝试这样做的方式:
初始设置视图控制器:
class SettingsVC2: UITableViewController{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.section == 0{
let cell = tableView.dequeueReusableCellWithIdentifier("office_cell", forIndexPath: indexPath)
cell.textLabel?.text = "Select your office"
return cell
}
else {
let cell = tableView.dequeueReusableCellWithIdentifier("dept_cell", forIndexPath: indexPath)
cell.textLabel?.text = "Select your department"
return cell
}
设置tableViews之一:
class SettingsDepartmentTVC: UITableViewController{
let departamentos: [String] = ["Sales", "Pre-Sales", "General Administration", "Customer Service", "Profesional Services", "Regionales"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return departamentos.count
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("department_cell", forIndexPath: indexPath)
cell.textLabel?.text = self.departamentos[indexPath.row]
return cell
}
}
我相信所有数据源和委托都已正确连接,即使未显示 UIKit 也已导入。
我得到的错误:
-[UITableView 中的断言失败 dequeueReusableCellWithIdentifier:forIndexPath:]
由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无法使单元格出列 带有标识符 office_cell - 必须为 标识符或连接情节提要中的原型单元
这是我尝试填充 tableView 的方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("CommentsRowTVC", forIndexPath: indexPath) as! CommentsRowTVC
let single_comment = self.AOS[indexPath.row]
cell.titulo_comentario?.text = single_comment.titulo_comment
cell.cuerpo_comentario?.text = single_comment.cuerpo_comment
cell.fecha_comentario?.text = single_comment.fecha_comment
return cell
}
class CommentsRowTVC: UITableViewCell {
@IBOutlet weak var titulo_comentario: UILabel!
@IBOutlet weak var cuerpo_comentario: UILabel!
@IBOutlet weak var fecha_comentario: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
这是表所在的 UIViewController 的连接检查器:
还有这个用于 UIViewCellController:
到此为止的错误:
UIView tableView:numberOfRowsInSection: 无法识别的选择器发送到实例
由于未捕获的异常 NSInvalidArgumentException 导致应用程序终止,原因:[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance
【问题讨论】:
-
可以添加storyBoard截图吗?
-
您是否在情节提要中添加了标识符?
-
@PabloDuque 你应该设置一个断点并尝试单步执行你的代码,看看它在哪里中断并报告
-
它必须在情节提要中,因为错误与 cellForRowAtIndexPath 无关。或者,如果它与该方法相关,则与 ID 或标识符相关。我编辑了添加 UITableViewCell 的答案。
标签: ios swift uitableview tableview