【问题标题】:Errors with Static Table Views静态表视图的错误
【发布时间】: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


【解决方案1】:

如果您真的要制作 static 表格视图单元格,那么您应该删除这些方法。仅在动态出列单元格时才使用它们。

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

}

如果您实际上是在尝试制作动态单元格,那么正如您的错误消息所示,您需要确保您的标识符与您在界面生成器中的标识符相匹配:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("{your_identifier}", forIndexPath: indexPath)
    cell.textLabel?.text = self.departamentos[indexPath.row]
    return cell

}

【讨论】:

  • 那么我该如何向单元格添加内容呢?
  • 如果你正在制作静态单元格,你可以在情节提要中进行。只需将标签拖出并命名为您想要的名称。 “静态”意味着它们永远不会改变,因此只需将标签拖出即可,但前提是您删除了我提到的方法。
  • 好吧,我明白了。表格视图的标签显示存储在 NSUserDefautls 中的内容怎么样?会不会是我想要做的工作?
  • 是的,只需将每个静态单元格中的插座拖到 tableViewController 中,然后从 NSUserDefaults 中提取值并将它们设置为 viewDidLoad 中的标签。
  • 好的,谢谢!我会尝试这两件事并在完成后回复您! :)
【解决方案2】:

使用您提供的代码,您似乎还没有在表格视图中注册表格视图单元格和标识符。

UITableView Class Reference

讨论在出列任何单元格之前,请调用此方法或 registerNib:forCellReuseIdentifier: 方法告诉表视图如何 创建新的细胞。如果指定类型的单元格当前不是 在重用队列中,表视图使用提供的信息 自动创建一个新的单元格对象。

如果您之前注册了具有相同重用性的类或 nib 文件 标识符,您在 cellClass 参数中指定的类替换 旧条目。如果您愿意,可以为 cellClass 指定 nil 从指定的重用标识符中注销类。

我建议你在viewDidLoad()注册。

    let KDepartmentCellIdentifier = "department_cell"
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kKDepartmentCellIdentifier)

    let KOfficeCellIdentifier = "office_cell"
    tableView.registerClass(UITableViewCell.class, forCellReuseIdentifier: kOfficeCellIdentifier)

如果您使用的是笔尖,请使用

registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 2014-04-17
    • 1970-01-01
    相关资源
    最近更新 更多