Ios UITableViewCell异常。EXC_断点,子代码=0xdefe

Ios UITableViewCell异常。EXC_断点,子代码=0xdefe,ios,uitableview,swift,Ios,Uitableview,Swift,我正在创建自定义UITableViewCell,而不使用IB或故事板。UITableViewCell需要为字幕样式。 当我使用基类UITableViewCell时,我发现该单元格具有默认样式,而不是我需要的字幕样式。 我认为这是因为在tableview:cellforrowatindexpath中,方法dequeueReusableCellWithIdentifier返回默认类型的单元格,可能这是因为类UITableViewCell的init方法返回默认样式 因此,我决定将UITableView

我正在创建自定义UITableViewCell,而不使用IB或故事板。UITableViewCell需要为字幕样式。 当我使用基类UITableViewCell时,我发现该单元格具有默认样式,而不是我需要的字幕样式。 我认为这是因为在tableview:cellforrowatindexpath中,方法dequeueReusableCellWithIdentifier返回默认类型的单元格,可能这是因为类UITableViewCell的init方法返回默认样式

因此,我决定将UITableViewCell子类化,并在子类MyTableViewCell的init方法中,将样式设置为Subtitle类型

主要代码段如下所示:

class MyTableViewCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "CellSubtitle")
    }

    required init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }
....
}
在tableviewcontroller中:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> MyTableViewCell! {
        let cellIdentifier = "CellSubtitle"

        var cell = tableView!.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyTableViewCell!

        if cell == nil   {
            println("Cell is nil so creating a new one")
            cell = MyTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier)
        }

        // Configure the cell...
        if indexPath.row == 3 {
            cell.textLabel.text = " Some text"
            cell.detailTextLabel.text = " Some more text!"
        }
        return cell
    }
这似乎在对单元格进行排队时引发异常,如下所示: 任何指向根本原因的指针都是非常感谢的

(lldb) bt
* thread #1: tid = 0x50b5f, 0x001de43e libswiftCore.dylib`swift_dynamicCastClassUnconditional + 26, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe)
  * frame #0: 0x001de43e libswiftCore.dylib`swift_dynamicCastClassUnconditional + 26
    frame #1: 0x00082754 swift2`swift2.MyTableViewController.tableView (tableView=Some, indexPath=Some, self=0x15e4d980)(Swift.ImplicitlyUnwrappedOptional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSIndexPath>) -> Swift.ImplicitlyUnwrappedOptional<swift2.MyTableViewCell> + 1244 at MyTableViewController.swift:52
    frame #2: 0x000835c4 swift2`@objc swift2.MyTableViewController.tableView (swift2.MyTableViewController)(Swift.ImplicitlyUnwrappedOptional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.ImplicitlyUnwrappedOptional<ObjectiveC.NSIndexPath>) -> Swift.ImplicitlyUnwrappedOptional<swift2.MyTableViewCell> + 100 at MyTableViewController.swift:0
    frame #3: 0x3139e8f6 UIKit`-[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 410
    frame #4: 0x31345c26 UIKit`-[UITableView _updateVisibleCellsNow:] + 1806

编辑:如果我将dequeueReusableCellWithIdentifier注释掉,崩溃就会消失

我遇到了一个类似的问题,在对集合视图单元格进行排队时,遇到了同样难以理解的涉及swift_DynamicCastClassConditional的错误。可能是其他人也犯了同样的错误,下面是发生在我身上的事情:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> MyTableViewCell! {
    //let cellIdentifier = "CellSubtitle"

    var cell = tableView!.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as MyTableViewCell!
 cell = MyTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: cellIdentifier);

    if cell == nil   {
        println("Cell is nil so creating a new one");
 NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@" your CustomCellnib name" owner:nil options:nil];


    }

    // Configure the cell...
    if indexPath.row == 3 {
        cell.textLabel.text = " Some text"
        cell.detailTextLabel.text = " Some more text!"
    }
    return cell
}
and make sure that ur nib is a subclass of MyTableViewcell..in Storyboar or in IB

在我的例子中,我有两个cell类,每个都有一个xib和一个swift类。其中一个是另一个的子类,是导致崩溃的那个。最后,我从超类中复制了xib以向其中添加部件,并且未能将Interface Builder中Identity Inspector中的单元格类从“超类”更改为“子类”。狡猾的疏忽!但这就解释了选角错误…

您是否在故事板中使用原型单元?如果是,您选择了什么样式?@Paulw11我想在不使用故事板或XIB文件的情况下执行此操作。所以故事板中没有原型单元。您在tableView上调用过registerClass:forCellReuseIdentifier:吗?如果您有,您永远不应该从dequeueReuseableCellWithIdentifier:forIndexPath:收到nil单元格。@Paulw11是的,我在viewdidload中收到了registerClass。是的,让我惊讶的是,我从未从dequeueReusableCellWithIdentifier收到过一个nil单元格[在我子类化之前,子类化之后我看到了异常],如果是这样的话,有没有一个方法来更新这个方法返回的单元格的UITableViewCellStyle。如果有,那么我就不需要子类化。我只是创建了一个复制您的代码的最小应用程序,它工作正常。在退出QueueReusableCellWithIdentifier之后,假设我们得到一个cell实例。无法调用MyTableViewCellstyle:…,reuseIdentifier:。。。然后创建一个新实例,而不是使用DequeueReusables…返回的实例。。。?如果cell==nil,您建议执行loadNibNamed,我想在不使用IB或故事板的情况下执行此操作,loadNibNamed将需要我创建一个xib文件,对吗?