【问题标题】:Subclassing UITableViewCell on Swift - Error edit UITableView rows在 Swift 上继承 UITableViewCell - 编辑 UITableView 行时出错
【发布时间】:2014-08-11 14:30:33
【问题描述】:

使用 Swift:我已将 NSFetchedResultsController 连接到 UITableView,并为 TableView 单元格创建了 UITableViewCells 的子类。

UITableViewCells 的格式和显示正确。但是,一旦我尝试编辑行(添加或删除),应用程序就会崩溃。

为了测试,我使用了最基本的子类:一个空的子类,它仍然崩溃。原来的 UITableViewCell 工作正常。

有什么想法吗?迅捷错误?崩溃直接进入堆栈顶部。我在控制台中什么也没有。

作品:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
    cell.textLabel.text = task.desc
    return cell
}

不起作用:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell2
    let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
    cell.textLabel.text = task.desc
    return cell
}

当子类是:

import UIKit

class UITableViewCell2: UITableViewCell {

}

【问题讨论】:

    标签: uitableview swift


    【解决方案1】:

    如果您以编程方式(而不是在 InterfaceBuilder 中)创建表格单元格,则必须向其中注册表格单元格类

    TableView.registerClass(..., forCellReuseIdentifier...)
    

    【讨论】:

    • 我创建了一个,但是发错了!这就是 UITableViewCell 起作用的原因,而不是 UITableViewCell2 的原因......谢谢你,这就是解决方案。
    • 要记住的另一件事是在此方法上编写 UITableViewCell 的初始化: override init(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: style, reuseIdentifier: reuseIdentifier) }
    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多