UITableView错误返回自定义原型单元格

时间:2015-09-10 23:18:32

标签: swift uitableview

'这个编码器要求从initWithCoder返回被替换的对象:'

我正在为此而努力。的UITableViewController -

这是cellForRow ..它调用func以根据索引返回适当的单元格。单元格是动态原型单元,正确设置了重用标识符

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        if tableView == self.tableView {
            switch indexPath.row {
            case 0: return self.imageCellForTableView(tableView, cellForRowAtIndexPath: indexPath)
            case 1: return self.descriptionCellForTableView(tableView, cellForRowAtIndexPath: indexPath)
            case 2: return self.buttonsCellForTableView(tableView, cellForRowAtIndexPath: indexPath)
            case 3: return self.cartCellForTableView(tableView, cellForRowAtIndexPath: indexPath)
            case 4: return self.detailsCellForTableView(tableView, cellForRowAtIndexPath: indexPath)
            default: print("Error", terminator: "")
            }
        } 
}

这是抛出错误的函数:

func cartCellForTableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

  // ERROR ON THE FOLLOWING LINE
    let cell = tableView.dequeueReusableCellWithIdentifier("addtocart", forIndexPath: indexPath)
    let btnAddToCart = cell.viewWithTag(4) as! UIButton
    btnAddToCart.layer.borderColor = buttonBorderColor.CGColor
    btnAddToCart.layer.borderWidth = buttonBorderWidth
    btnAddToCart.setTitleColor(GOLD, forState: UIControlState.Normal)
    btnAddToCart.backgroundColor = MAROON
    btnAddToCart.addTarget(self, action: "addItemToCartAndBuy:", forControlEvents: UIControlEvents.TouchUpInside)

    let btnApplePay = cell.viewWithTag(40) as! UIButton
    btnApplePay.addTarget(self, action: "applePay", forControlEvents: UIControlEvents.TouchUpInside)
    return cell
}

这个函数,imageCellForTableView:工作正常

func imageCellForTableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("image", forIndexPath: indexPath) 
    let imgItem = cell.viewWithTag(1) as! PFImageView
    if let imgFile: PFFile = item.itemImage {
        imgItem.file = imgFile
        imgItem.loadInBackground({ (image: UIImage?, error: NSError?) -> Void in
            self.imgVToAnimate = PFImageView(frame: imgItem.convertRect(imgItem.frame, toView: self.navigationController?.view))
            self.imgVToAnimate.file = imgFile
            self.imgVToAnimate.loadInBackground()
        })
    } else {
        imgItem.image = UIImage(named: "StoreError")
        imgVToAnimate = PFImageView(frame: imgItem.convertRect(imgItem.frame, toView: self.navigationController?.view))
        imgVToAnimate.image = UIImage(named: "StoreError")
    }
    imgVToAnimate.frame = imgItem .convertRect(imgItem.frame, toView: self.navigationController?.view)
    imgVToAnimate.contentMode = UIViewContentMode.ScaleAspectFit
    imgOriginal = imgItem
    return cell
}

这是错误:

2015-09-10 14:45:10.705 Corpsboard[89830:1947577] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'This coder requires that replaced objects be returned from initWithCoder:'
        *** First throw call stack:
        (
            0   CoreFoundation                      0x000000010a1519b5 __exceptionPreprocess + 165
            1   libobjc.A.dylib                     0x0000000109bc5deb objc_exception_throw + 48
            2   CoreFoundation                      0x000000010a1518ed +[NSException raise:format:] + 205
            3   UIKit                               0x000000010800d6d1 UINibDecoderDecodeObjectForValue + 780
            4   UIKit                               0x000000010800d847 UINibDecoderDecodeObjectForValue + 1154
            5   UIKit                               0x000000010800d3bc -[UINibDecoder decodeObjectForKey:] + 278
            6   UIKit                               0x0000000107b4cdae -[UIView initWithCoder:] + 898
            7   UIKit                               0x000000010805e4ab -[UITableViewCellContentView initWithCoder:] + 40
            8   UIKit                               0x000000010800d686 UINibDecoderDecodeObjectForValue + 705
            9   UIKit                               0x000000010800d847 UINibDecoderDecodeObjectForValue + 1154
            10  UIKit                               0x000000010800d3bc -[UINibDecoder decodeObjectForKey:] + 278
            11  UIKit                               0x0000000107b4cdae -[UIView initWithCoder:] + 898
            12  UIKit                               0x0000000107e08689 -[UITableViewCell initWithCoder:] + 93
            13  UIKit                               0x000000010800d686 UINibDecoderDecodeObjectForValue + 705
            14  UIKit                               0x000000010800d847 UINibDecoderDecodeObjectForValue + 1154
            15  UIKit                               0x000000010800d3bc -[UINibDecoder decodeObjectForKey:] + 278
            16  UIKit                               0x0000000107e55a6c -[UINib instantiateWithOwner:options:] + 1290
            17  UIKit                               0x0000000107bfde91 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 388
            18  UIKit                               0x0000000107bfe263 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 71
            19  Corpsboard                          0x000000010538b574 _TFC10Corpsboard28StoreItemTableViewController20cartCellForTableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 132
            20  Corpsboard                          0x000000010538556a _TFC10Corpsboard28StoreItemTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 986
            21  Corpsboard                          0x000000010538711f _TToFC10Corpsboard28StoreItemTableViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
            22  UIKit                               0x0000000107c100c6 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
            23  UIKit                               0x0000000107c101db -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
            24  UIKit                               0x0000000107be6250 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3187
            25  UIKit                               0x0000000107c18fa8 -[UITableView _performWithCachedTraitCollection:] + 92
            26  UIKit                               0x0000000107c01599 -[UITableView layoutSubviews] + 218
            27  UIKit                               0x0000000107b72a3b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
            28  QuartzCore                          0x000000010784536a -[CALayer layoutSublayers] + 146
            29  QuartzCore                          0x0000000107839bd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
            30  QuartzCore                          0x0000000107839a4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
            31  QuartzCore                          0x000000010782e1d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
            32  QuartzCore                          0x000000010785b9f0 _ZN2CA11Transaction6commitEv + 508
            33  UIKit                               0x0000000107ac0556 _UIApplicationHandleEventQueue + 7183
            34  CoreFoundation                      0x000000010a07db21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
            35  CoreFoundation                      0x000000010a073a4c __CFRunLoopDoSources0 + 556
            36  CoreFoundation                      0x000000010a072f03 __CFRunLoopRun + 867
            37  CoreFoundation                      0x000000010a072918 CFRunLoopRunSpecific + 488
            38  GraphicsServices                    0x000000010bc16ad2 GSEventRunModal + 161
            39  UIKit                               0x0000000107ac599e UIApplicationMain + 171
            40  Corpsboard                          0x00000001052bdfef main + 111
            41  libdyld.dylib                       0x000000010b14792d start + 1
            42  ???                                 0x0000000000000001 0x0 + 1
        )
        libc++abi.dylib: terminating with uncaught exception of type NSException
        (lldb) 

enter image description here

1 个答案:

答案 0 :(得分:0)

嗯..尝试删除if tableView == self.tableView条件......

... tableView:cellForRowAtIndexPath:方法已经在非零表视图中传递,因此您不需要这样做。

另一方面,您有没有使用静态表视图控制器的原因?如果你的表只有这5行,那么仅仅一个静态表可能要容易得多,并且将连接拖到视图控制器上(所以你不必使用viewWithTag等)它可能会使这个控制器更简单!