UITableViewCell崩溃

时间:2018-12-07 04:47:29

标签: ios iphone swift uitableview

我的代码中发生了奇怪的崩溃,该崩溃在准备UITableViewCell时崩溃了。崩溃报告不令人满意的原因是什么?

Crashed: com.apple.main-thread
    0  Quickride                      0x104c2434c specialized NotificationViewController.tableView(_:cellForRowAt:) (NotificationViewController.swift:222)
    1  Quickride                      0x104c1fdcc @objc NotificationViewController.tableView(_:cellForRowAt:) (NotificationViewController.swift)
    2  UIKitCore                      0x1c7abc540 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 684
    3  UIKitCore                      0x1c7abca88 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 80
    4  UIKitCore                      0x1c7a88160 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2256
    5  UIKitCore                      0x1c7aa604c -[UITableView layoutSubviews] + 140
    6  UIKitCore                      0x1c7d3aea4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1380
    7  QuartzCore                     0x19ecbac70 -[CALayer layoutSublayers] + 184
    8  QuartzCore                     0x19ecbfc00 CA::Layer::layout_if_needed(CA::Transaction*) + 324
    9  UIKitCore                      0x1c7d26390 -[UIView(Hierarchy) layoutBelowIfNeeded] + 548
    10 UIKitCore                      0x1c723a6d0 -[UINavigationController _layoutViewController:] + 1296
    11 UIKitCore                      0x1c7234dc4 -[UINavigationController _layoutTopViewController] + 236
    12 UIKitCore                      0x1c7232f94 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 780
    13 UIKitCore                      0x1c7cf5fd8 -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 420
    14 UIKitCore                      0x1c7cf6294 -[UINavigationTransitionView _cleanupTransition] + 588
    15 UIKitCore                      0x1c7d04154 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 312
    16 UIKitCore                      0x1c7cffb90 +[UIViewAnimationState popAnimationState] + 336
    17 UIKitCore                      0x1c7cf5d1c -[UINavigationTransitionView transition:fromView:toView:] + 1864
    18 UIKitCore                      0x1c723b4e0 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2656
    19 UIKitCore                      0x1c723babc -[UINavigationController _startDeferredTransitionIfNeeded:] + 1184
    20 UIKitCore                      0x1c723cdbc -[UINavigationController __viewWillLayoutSubviews] + 164
    21 UIKitCore                      0x1c721efac -[UILayoutContainerView layoutSubviews] + 224
    22 UIKitCore                      0x1c7d3aea4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1380
    23 QuartzCore                     0x19ecbac70 -[CALayer layoutSublayers] + 184
    24 QuartzCore                     0x19ecbfc00 CA::Layer::layout_if_needed(CA::Transaction*) + 324
    25 QuartzCore                     0x19ec1e718 CA::Context::commit_transaction(CA::Transaction*) + 340
    26 QuartzCore                     0x19ec4d04c CA::Transaction::commit() + 608
    27 QuartzCore                     0x19ec4deb4 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92
    28 CoreFoundation                 0x19a6577a8 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
    29 CoreFoundation                 0x19a65243c __CFRunLoopDoObservers + 412
    30 CoreFoundation                 0x19a6529dc __CFRunLoopRun + 1264
    31 CoreFoundation                 0x19a6521cc CFRunLoopRunSpecific + 436
    32 GraphicsServices               0x19c8c9584 GSEventRunModal + 100
    33 UIKitCore                      0x1c7895054 UIApplicationMain + 212
    34 Quickride                      0x104b9968c main (AppDelegate.swift:29)
    35 libdyld.dylib                  0x19a112bb4 start + 4

以下是发生崩溃的代码:-

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  if notifications.count <= indexPath.row{
    return tableView.dequeueReusableCell(withIdentifier: "non_action") as!NotificationWithActionTableViewCell
  }

    let notification = notifications[indexPath.row]
    let notificationHandler = NotificationHandlerFactory.getNotificationHandler(clientNotification: notification)

    var cell:NotificationWithActionTableViewCell?

    if notification.isActionRequired == true && notification.isActionTaken == false{
      if (notificationHandler!.getPositiveActionNameWhenApplicable(userNotification: notification) != nil || notificationHandler!.getNegativeActionNameWhenApplicable(userNotification: notification) != nil || notificationHandler!.getNeutralActionNameWhenApplicable(userNotification: notification) != nil){
        cell = tableView.dequeueReusableCell(withIdentifier: "action") as? NotificationWithActionTableViewCell
        handleUnReadNotification(cell: cell!, notification: notification,notificationHandler : notificationHandler!, indexPath : indexPath as NSIndexPath)
      }else{
        cell = tableView.dequeueReusableCell(withIdentifier: "non_action") as? NotificationWithActionTableViewCell

        cell!.iboDetail.textColor = UIColor(netHex: 0x3c3c3c)
        cell!.notifictionRowBackGround.backgroundColor = UIColor(netHex: 0xffffcc)
      }

    }else{
        cell = tableView.dequeueReusableCell(withIdentifier: "non_action") as? NotificationWithActionTableViewCell
        handleReadNotification(cell: cell!, notification: notification)
    }
    cell!.setNotificationIconAndTitle(notificationHandler: notificationHandler, notification: notification)
    cell!.iboTimeStamp.text = DateUtils.getTimeStringFromTimeInMillis(timeStamp: Double(notification.time!)*1000, timeFormat: DateUtils.DATE_FORMAT_dd_MMM_hh_mm_aaa)
    if notification.sendFrom != 0{
        cell?.iboNotificationIcon?.isUserInteractionEnabled = true
        cell?.iboNotificationIcon?.tag = indexPath.row
        cell?.iboNotificationIcon?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(NotificationViewController.notificaitonIconTapped(_:))))
    }
    else{
       cell?.iboNotificationIcon?.isUserInteractionEnabled = false
    }

    return cell!

}

崩溃是从上述代码的以下行发生的:-

 cell!.iboDetail.textColor = UIColor(netHex: 0x3c3c3c)

任何解决方案的人。尝试了很多但无法解决问题。预先感谢

1 个答案:

答案 0 :(得分:3)

崩溃的原因是在上一行

cell = tableView.dequeueReusableCell(withIdentifier: "non_action") as? NotificationWithActionTableViewCell

...您最终得到cellnil。当表格视图无法出队时,您必须自己创建单元格。

当然,如果您像其他所有人一样打电话给dequeueReusableCell(withIdentifier:forIndexPath:),那么您将永远不会得到nil并且不会出现问题。