com.apple.main-thread在cellForRowAt中的EXC_BAD_ACCESS KERN_INVALID_ADDRESS

时间:2018-02-06 08:22:56

标签: ios swift uitableview kern-invalid-address

我在cellForRow中经历过这次崩溃.Crashlytics指出的行号是:

cell.table = questionTable

该行周围的整个案例是:

case .mcq_single:
if let cell = tableView.dequeueReusableCell(withIdentifier: "IQ_MCQCell") as? IQ_MCQCell
{
       cell.table = questionTable;
       cell.delegate = self;
       cell.qID = answerIDs![indexPath.row];
       cell.selectionStyle = .none

       if questionNA[indexPath.row]
       {
           cell.NABox.setImage(#imageLiteral(resourceName: "checkbox_selected"), for: .normal);
           cell.questionItem.isHidden = true;
       } else {
           cell.NABox.setImage(#imageLiteral(resourceName: "checkbox_up"), for: .normal);
           cell.questionItem.isHidden = false;
       }

       let cell2 = cell.formatCell(with: item, for: cell, at: indexPath)
       textHeight[indexPath.row] = cell.questionText.numberOfVisibleLines
       return cell2;
}


Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x10281a7e8 swift_unknownRetain + 8
1  AppName                        0x100f24b48 specialized InspectionItemVC.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell (InspectionItemVC.swift:887)
2  FlexConnect                    0x100f1f2b8 @objc InspectionItemVC.tableView(UITableView, cellForRowAt : IndexPath) -> UITableViewCell (InspectionItemVC.swift)
3  UIKit                          0x18f537ac4 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
4  UIKit                          0x18f538028 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]
5  UIKit                          0x18f5168ac -[UITableView _updateVisibleCellsNow:isRecursive:]
6  UIKit                          0x18f2cd33c -[UITableView layoutSubviews]
7  UIKit                          0x18f1f9f00 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
8  QuartzCore                     0x189c89998 -[CALayer layoutSublayers]
9  QuartzCore                     0x189c8db20 CA::Layer::layout_if_needed(CA::Transaction*)
10 UIKit                          0x18f20eccc -[UIView(Hierarchy) layoutBelowIfNeeded]
11 UIKit                          0x18f2a46f4 -[UINavigationController _layoutViewController:]
12 UIKit                          0x18f2a1dc4 -[UINavigationController _layoutTopViewController]
13 UIKit                          0x18f2bac64 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:]
14 UIKit                          0x18f2ba908 -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:]
15 UIKit                          0x18f2ba4e0 -[UINavigationTransitionView _cleanupTransition]
16 UIKit                          0x18f234bb4 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
17 UIKit                          0x18f232cb8 +[UIViewAnimationState popAnimationState]
18 UIKit                          0x18f2adc48 -[UINavigationTransitionView transition:fromView:toView:]
19 UIKit                          0x18f2a3d24 -[UINavigationController _startTransition:fromViewController:toViewController:]
20 UIKit                          0x18f2a2e14 -[UINavigationController _startDeferredTransitionIfNeeded:]
21 UIKit                          0x18f2a2890 -[UINavigationController __viewWillLayoutSubviews]
22 UIKit                          0x18f2a2790 -[UILayoutContainerView layoutSubviews]
23 UIKit                          0x18f1f9f00 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
24 QuartzCore                     0x189c89998 -[CALayer layoutSublayers]
25 QuartzCore                     0x189c8db20 CA::Layer::layout_if_needed(CA::Transaction*)
26 QuartzCore                     0x189bfa36c CA::Context::commit_transaction(CA::Transaction*)
27 QuartzCore                     0x189c21b90 CA::Transaction::commit()
28 UIKit                          0x18f1ef5c8 _afterCACommitHandler
29 CoreFoundation                 0x185bededc __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__
30 CoreFoundation                 0x185beb894 __CFRunLoopDoObservers
31 CoreFoundation                 0x185bebe50 __CFRunLoopRun
32 CoreFoundation                 0x185b0be58 CFRunLoopRunSpecific
33 GraphicsServices               0x1879b8f84 GSEventRunModal
34 UIKit                          0x18f26067c UIApplicationMain
35 AppName                        0x100094590 main (AppDelegate.swift:23)
36 libdyld.dylib                  0x18562856c start

更新

weak var delegate: inspQDelegate? 
// weak added here
var indexPath: IndexPath?
//var table   : UITableView?; table removed indexPath used instead.

inspQDelegate的定义如下:

public protocol inspQDelegate
{
    func setNA(at index: IndexPath);
    func setAnswer(at index: IndexPath, value: String);
    func setComment(at index: IndexPath, value: String);
    func scrollToMe(at index: IndexPath)
}

更新2: 当inspQdelegate的定义如下:

public protocol inspQDelegate: NSObjectProtocol
{
    func setNA(at index: IndexPath);
    func setAnswer(at index: IndexPath, value: String);
    func setComment(at index: IndexPath, value: String);
    func scrollToMe(at index: IndexPath)
}

我在行上收到了一个新的崩溃:cell.delegate = self下面:

case .true_false:
     if let cell = tableView.dequeueReusableCell(withIdentifier: "IQ_FlipCell") as? IQ_FlipCell {
            cell.table = questionTable;
            cell.delegate = self;
            cell.qID = answerIDs![indexPath.row];
            cell.selectionStyle = .none

            if questionNA[indexPath.row]
            {
                cell.NABox.setImage(#imageLiteral(resourceName: "checkbox_selected"), for: .normal);
                cell.questionItem.isHidden = true;
            } else {
                cell.NABox.setImage(#imageLiteral(resourceName: "checkbox_up"), for: .normal);
                cell.questionItem.isHidden = false;
            }

            let cell2 = cell.formatCell(with: item, for: cell, at: indexPath, qtype: 0)
            textHeight[indexPath.row] = cell.questionText.numberOfVisibleLines
            return cell2;
     }

但定义如下:

public protocol inspQDelegate: class
{...}

我没有收到这个新的崩溃

1 个答案:

答案 0 :(得分:0)

看起来你正在tableView和它的单元格之间创建一个保留周期。单元格经常出列并重复使用,因此您不应使用强引用。您的单元格有一个委托,无论如何都设置为tableView,因此您不需要设置cell.tableView,只需使用委托。

在单元格中声明委托和tableView时。 (我建议不要使用tableView属性)确保使用弱引用,这应该可以解决您的问题。

weak var delegate: MyTableViewDelegateProtoocol?
weak var tableView: UITableView?

尝试一下,让我知道结果

这是一个sendgrid-csharp,有助于解释Swift中的保留周期和内存管理

更新:

public protocol inspQDelegate: class
{
    func setNA(at index: IndexPath);
    func setAnswer(at index: IndexPath, value: String);
    func setComment(at index: IndexPath, value: String);
    func scrollToMe(at index: IndexPath)
}