2017-03-31 129 views
0

我遇到了這個問題,當我調用popover tableview時,應用程序崩潰。代碼和錯誤圖片如下。我不知道還有什麼要寫的,我只是找不到一個錯誤。 我注意到titleForHeaderInSection多次調用numberOfRowsInSection crash in titleForHeaderInSection

protocol SelectChatTableViewControllerDelegate { 
    func dissmisPopover() 
    func groupDidSelected(row: Int) 
} 

class SelectChatTableViewController: UITableViewController { 

    var delegate: SelectChatTableViewControllerDelegate? 
    let defaults = UserDefaults.standard 
    var newChatsIds: [String]? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     newChatsIds = defaults.array(forKey: "newChatsIds") as? [String] 
    } 

    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 2 
    } 

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     if section == 0 { 
      if tableView.numberOfRows(inSection: 0) == 0 { 
       return nil 
      } else { 
       return "Invites" 
      } 
     } else { 
      if tableView.numberOfRows(inSection: 1) == 0 { // Crash 
       return nil 
      } else { 
       return "Rooms" 
      } 
     } 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     if section == 0 { 
      return newChatsIds!.count 
     } else { 
      return 1 
     } 
    } 

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     tableView.deselectRow(at: indexPath, animated: true) 
     if let delegate = delegate { 
      delegate.groupDidSelected(row: indexPath.row) 
     } 
    } 

    override func viewDidDisappear(_ animated: Bool) { 
     super.viewDidDisappear(animated) 
     if let delegate = delegate { 
      delegate.dissmisPopover() 
     } 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) 
     if indexPath.section == 0 { 
      cell.textLabel?.text = newChatsIds?[indexPath.row] 
     } else { 
      cell.textLabel?.text = "123" 
     } 
     // Configure the cell... 

     return cell 
    } 

UPD線程的堆棧跟蹤

Stack trace (
     (
     "0 Real        0x000000010107983d _TFC4Real29SelectChatTableViewController9tableViewfTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 285", 
     "1 Real        0x0000000101079b79 _TToFC4Real29SelectChatTableViewController9tableViewfTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 73", 
     "2 UIKit        0x00000001046cd086 -[UITableView _delegateWantsHeaderTitleForSection:] + 73", 
     "3 UIKit        0x00000001046cd038 -[UITableView _delegateWantsHeaderForSection:] + 711", 
     "4 UIKit        0x000000010491544d -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162", 
     "5 UIKit        0x000000010491bd99 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 501", 
     "6 UIKit        0x000000010491bef7 -[UITableViewRowData heightForTable] + 56", 
     "7 UIKit        0x000000010469dd76 -[UITableView _updateContentSize] + 381", 
     "8 UIKit        0x00000001046c37bc -[UITableView _rectChangedWithNewSize:oldSize:] + 1105", 
     "9 UIKit        0x00000001046c3f04 -[UITableView setFrame:] + 318", 
     "10 UIKit        0x000000010460c39a -[UIView(Geometry) _applyAutoresizingMaskWithOldSuperviewSize:] + 998", 
     "11 UIKit        0x000000010460d3f7 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 264", 
     "12 UIKit        0x0000000104fccd6a -[UIScrollView(_UIOldConstraintBasedLayoutSupport) _resizeWithOldSuperviewSize:] + 47", 
     "13 CoreFoundation      0x0000000106bc40b2 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 114", 
     "14 CoreFoundation      0x0000000106bc3fcf -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 335", 
     "15 UIKit        0x000000010460bbe7 -[UIView(Geometry) resizeSubviewsWithOldSize:] + 184", 
     "16 UIKit        0x000000010460a0a0 -[UIView(Geometry) setFrame:] + 1110", 
     "17 UIKit        0x0000000104e23f20 -[_UIPopoverView layoutSubviews] + 253", 
     "18 UIKit        0x000000010461e239 +[UIView(Animation) performWithoutAnimation:] + 90", 
     "19 UIKit        0x0000000104e23bc9 -[_UIPopoverView willMoveToWindow:] + 389", 
     "20 UIKit        0x00000001046150b8 -[UIView(Hierarchy) _willMoveToWindow:] + 890", 
     "21 UIKit        0x000000010462585b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 451", 
     "22 UIKit        0x00000001046140d7 -[UIView(Hierarchy) addSubview:] + 838", 
     "23 UIKit        0x00000001050db57b __67-[UIViewControllerBuiltinTransitionViewAnimator animateTransition:]_block_invoke + 49", 
     "24 UIKit        0x000000010461f5c0 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 581", 
     "25 UIKit        0x000000010461fad9 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 105", 
     "26 UIKit        0x00000001050dad4a -[UIViewControllerBuiltinTransitionViewAnimator animateTransition:] + 1798", 
     "27 UIKit        0x00000001046e5a8c __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 2915", 
     "28 UIKit        0x000000010457162f _runAfterCACommitDeferredBlocks + 320", 
     "29 UIKit        0x000000010455e273 _cleanUpAfterCAFlushAndRunDeferredBlocks + 566", 
     "30 UIKit        0x000000010458f746 _afterCACommitHandler + 176", 
     "31 CoreFoundation      0x0000000106bd9267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23", 
     "32 CoreFoundation      0x0000000106bd91d7 __CFRunLoopDoObservers + 391", 
     "33 CoreFoundation      0x0000000106bbdf8e __CFRunLoopRun + 1198", 
     "34 CoreFoundation      0x0000000106bbd884 CFRunLoopRunSpecific + 420", 
     "35 GraphicsServices     0x000000010a8faa6f GSEventRunModal + 161", 
     "36 UIKit        0x0000000104564c68 UIApplicationMain + 159", 
     "37 Real        0x00000001010a0edf main + 111", 
     "38 libdyld.dylib      0x0000000107ba868d start + 1", 
     "39 ???         0x0000000000000001 0x0 + 1" 
    ) 
) 
2017-03-31 18:20:57.333 Real[31480:752922] Stack trace (
     (
     "0 Real        0x000000010107983d _TFC4Real29SelectChatTableViewController9tableViewfTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 285", 
     "1 Real        0x0000000101079b79 _TToFC4Real29SelectChatTableViewController9tableViewfTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 73", 
     "2 UIKit        0x00000001046cd086 -[UITableView _delegateWantsHeaderTitleForSection:] + 73", 
     "3 UIKit        0x00000001046cd038 -[UITableView _delegateWantsHeaderForSection:] + 711", 
     "4 UIKit        0x000000010491544d -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162", 
     "5 UIKit        0x0000000104919d80 -[UITableViewRowData numberOfRowsInSection:] + 95", 
     "6 Real        0x00000001010798b3 _TFC4Real29SelectChatTableViewController9tableViewfTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 403", 
     "7 Real        0x0000000101079b79 _TToFC4Real29SelectChatTableViewController9tableViewfTCSo11UITableView23titleForHeaderInSectionSi_GSqSS_ + 73", 
     "8 UIKit        0x00000001046cd086 -[UITableView _delegateWantsHeaderTitleForSection:] + 73", 
     "9 UIKit        0x00000001046cd038 -[UITableView _delegateWantsHeaderForSection:] + 711", 
     "10 UIKit        0x000000010491544d -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 162", 
     "11 UIKit        0x000000010491bd99 -[UITableViewRowData rectForFooterInSection:heightCanBeGuessed:] + 501", 
     "12 UIKit        0x000000010491bef7 -[UITableViewRowData heightForTable] + 56", 
     "13 UIKit        0x000000010469dd76 -[UITableView _updateContentSize] + 381", 
     "14 UIKit        0x00000001046c37bc -[UITableView _rectChangedWithNewSize:oldSize:] + 1105", 
     "15 UIKit        0x00000001046c3f04 -[UITableView setFrame:] + 318", 
     "16 UIKit        0x000000010460c39a -[UIView(Geometry) _applyAutoresizingMaskWithOldSuperviewSize:] + 998", 
     "17 UIKit        0x000000010460d3f7 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 264", 
     "18 UIKit        0x0000000104fccd6a -[UIScrollView(_UIOldConstraintBasedLayoutSupport) _resizeWithOldSuperviewSize:] + 47", 
     "19 CoreFoundation      0x0000000106bc40b2 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 114", 
     "20 CoreFoundation      0x0000000106bc3fcf -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 335", 
     "21 UIKit        0x000000010460bbe7 -[UIView(Geometry) resizeSubviewsWithOldSize:] + 184", 
     "22 UIKit        0x000000010460a0a0 -[UIView(Geometry) setFrame:] + 1110", 
     "23 UIKit        0x0000000104e23f20 -[_UIPopoverView layoutSubviews] + 253", 
     "24 UIKit        0x000000010461e239 +[UIView(Animation) performWithoutAnimation:] + 90", 
     "25 UIKit        0x0000000104e23bc9 -[_UIPopoverView willMoveToWindow:] + 389", 
     "26 UIKit        0x00000001046150b8 -[UIView(Hierarchy) _willMoveToWindow:] + 890", 
     "27 UIKit        0x000000010462585b -[UIView(Internal) _addSubview:positioned:relativeTo:] + 451", 
     "28 UIKit        0x00000001046140d7 -[UIView(Hierarchy) addSubview:] + 838", 
     "29 UIKit        0x00000001050db57b __67-[UIViewControllerBuiltinTransitionViewAnimator animateTransition:]_block_invoke + 49", 
     "30 UIKit        0x000000010461f5c0 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 581", 
     "31 UIKit        0x000000010461fad9 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 105", 
     "32 UIKit        0x00000001050dad4a -[UIViewControllerBuiltinTransitionViewAnimator animateTransition:] + 1798", 
     "33 UIKit        0x00000001046e5a8c __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 2915", 
     "34 UIKit        0x000000010457162f _runAfterCACommitDeferredBlocks + 320", 
     "35 UIKit        0x000000010455e273 _cleanUpAfterCAFlushAndRunDeferredBlocks + 566", 
     "36 UIKit        0x000000010458f746 _afterCACommitHandler + 176", 
     "37 CoreFoundation      0x0000000106bd9267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23", 
     "38 CoreFoundation      0x0000000106bd91d7 __CFRunLoopDoObservers + 391", 
     "39 CoreFoundation      0x0000000106bbdf8e __CFRunLoopRun + 1198", 
     "40 CoreFoundation      0x0000000106bbd884 CFRunLoopRunSpecific + 420", 
     "41 GraphicsServices     0x000000010a8faa6f GSEventRunModal + 161", 
     "42 UIKit        0x0000000104564c68 UIApplicationMain + 159", 
     "43 Real        0x00000001010a0edf main + 111", 
     "44 libdyld.dylib      0x0000000107ba868d start + 1", 
     "45 ???         0x0000000000000001 0x0 + 1" 
    ) 
) 
+3

,你可以在線程的堆棧跟蹤看看,並張貼在這裏。也許有一個無限遞歸。 –

+0

您確定您當時有第1部分嗎?那可能是爲什麼? – pbodsk

+0

@pbodsk第1部分可在 –

回答

0
class SelectChatTableViewController: UITableViewController { 

    var newChatsIds: [String] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     newChatsIds = UserDefaults.standard.array(forKey: "newChatsIds") as? [String] ?? [] 
    } 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 2 
    } 

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     guard section == 1 || (section == 0 && !newChatsIds.isEmpty) else { return nil } 

     return [0: "Invites", 1: "Rooms"][section] 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return [0: newChatsIds.count][section] ?? 1 
    } 
} 

快速測試現在看來,這的確是通過訪問後引起:

tableView.numberOfRows(inSection:) 

取而代之的是直接檢查源代碼。


如果你不介意的標題是""你也可以使用:

return [0: newChatsIds.isEmpty ? "" : "Invites", 1: "Rooms"][section]