繁体   English   中英

UITableViewCell崩溃

[英]UITableViewCell crash

我的代码中有一个奇怪的崩溃,它在准备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)

任何解决方案的人都做了很多尝试,但无法解决问题。 提前致谢

崩溃的原因是在前一行

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

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

当然,如果你会叫dequeueReusableCell(withIdentifier:forIndexPath:) ,而不是像其他人一样,那么你将永远不会nil ,不会出现这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM