2017-03-31 11 views
0

この問題が発生しました。私はpopover tableviewを呼び出すとアプリケーションがクラッシュします。コードとエラーのある画像を以下に示します。私は他に何を書くべきか分からない、私はちょうどエラーを見つけることができない。 私はtitleForHeaderInSectionは簡単なテストの後numberOfRowsInSectionが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] 
関連する問題