2017-02-24 75 views
1

我的滾動到底部時,我的用戶崩潰太多。造成這個問題的原因是什麼? enter image description here滾動到底部的崩潰過多

我滾動到底部我的桌面視圖出現鍵盤時。這導致崩潰。我的代碼:

func keyboardWillShow(notification: NSNotification) { 
    var info = notification.userInfo! 
    let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 

    self.view.layoutIfNeeded() 
    UIView.animateWithDuration(0.2) { 
     self.bottomCons.constant = keyboardFrame.size.height 

     self.view.layoutIfNeeded() 
    } 
    let numberOfRows = tableView.numberOfRowsInSection(0) 
    if numberOfRows != 0 { 
     do { 
      try tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: numberOfRows-1, inSection: 0), atScrollPosition: .Bottom, animated: false) //This line is crashing 
     }catch { 
      print("error") 
     } 
    } 

} 

崩潰日誌:

Exception Type: EXC_BREAKPOINT (SIGTRAP) 
Exception Codes: 0x0000000000000001, 0x000000010013e018 
Termination Signal: Trace/BPT trap: 5 
Termination Reason: Namespace SIGNAL, Code 0x5 
Terminating Process: exc handler [0] 
Triggered by Thread: 0 

Thread 0 name: 
Thread 0 Crashed: 
0 Ribony       0x000000010013e018 specialized ChatViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell + 1904 (ChatViewController.swift:0) 
1 Ribony       0x0000000100135890 @objc ChatViewController.tableView(UITableView, cellForRowAtIndexPath : NSIndexPath) -> UITableViewCell + 76 (ChatViewController.swift:0) 
2 UIKit       0x000000018e315b58 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 716 (UITableView.m:10665) 
3 UIKit       0x000000018e315e00 -[UITableView _createPreparedCellForRowAtIndexPath:willDisplay:] + 88 (UITableView.m:10717) 
4 UIKit       0x000000018e31b4a8 -[UITableView _heightForRowAtIndexPath:] + 256 (UITableView.m:12615) 
5 UIKit       0x000000018e1127c4 -[UISectionRowData heightForRow:inSection:canGuess:] + 280 (UITableViewRowData.m:288) 
6 UIKit       0x000000018e4a413c -[UITableViewRowData ensureHeightsFaultedInForIndexPath:availHeight:edgeInset:scrollPosition:] + 1484 (UITableViewRowData.m:2418) 
7 UIKit       0x000000018e1ef4f8 -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:] + 1792 (UITableView.m:5544) 
8 UIKit       0x000000018e1eecb0 -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] + 52 (UITableView.m:5644) 
9 Ribony       0x000000010013ed00 specialized ChatViewController.keyboardWillShow(NSNotification) ->() + 800 (ChatViewController.swift:550) 
10 Ribony       0x0000000100136e98 @objc ChatViewController.keyboardWillShow(NSNotification) ->() + 56 (ChatViewController.swift:0) 
11 CoreFoundation     0x00000001880b6b10 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20 (CFNotificationCenter.c:650) 
12 CoreFoundation     0x00000001880b6214 _CFXRegistrationPost + 400 (CFNotificationCenter.c:164) 
13 CoreFoundation     0x00000001880b5f90 ___CFXNotificationPost_block_invoke + 60 (CFNotificationCenter.c:1031) 
14 CoreFoundation     0x0000000188125b8c -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1504 (CFXNotificationRegistrar.m:163) 
15 CoreFoundation     0x0000000187ff7e64 _CFXNotificationPost + 376 (CFNotificationCenter.c:1028) 
16 Foundation      0x0000000188b2ce0c -[NSNotificationCenter postNotificationName:object:userInfo:] + 68 (NSNotification.m:482) 
17 UIKit       0x000000018e9a6b40 -[UIInputWindowController postStartNotifications:withInfo:] + 400 (UIInputWindowController.m:1375) 
18 UIKit       0x000000018e9a8cf0 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke.907 + 388 (UIInputWindowController.m:1816) 
19 UIKit       0x000000018e0080f0 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 636 (UIView.m:11811) 
20 UIKit       0x000000018e0d952c +[UIView(UIViewAnimationWithBlocks) _animateWithDuration:delay:options:animations:start:completion:] + 128 (UIView.m:11865) 
21 UIKit       0x000000018e9a876c -[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:] + 1368 (UIInputWindowController.m:1877) 
22 UIKit       0x000000018e9af268 -[UIInputWindowController setInputViewSet:] + 1444 (UIInputWindowController.m:2923) 
23 UIKit       0x000000018e9a7e38 -[UIInputWindowController performOperations:withAnimationStyle:] + 56 (UIInputWindowController.m:1674) 
24 UIKit       0x000000018e099278 -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1276 (UIPeripheralHost.m:4647) 
25 UIKit       0x000000018dff8a78 -[UIResponder(UIResponderInputViewAdditions) reloadInputViews] + 80 (UIResponder.m:1107) 
26 UIKit       0x000000018e056b4c -[UIResponder becomeFirstResponder] + 600 (UIResponder.m:272) 
27 UIKit       0x000000018e056ebc -[UIView(Hierarchy) becomeFirstResponder] + 148 (UIView.m:9419) 
28 UIKit       0x000000018e0d90b4 -[UITextField becomeFirstResponder] + 60 (UITextField.m:1396) 
29 UIKit       0x000000018e180128 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 192 (UITextInteractionAssistant.m:1160) 
30 UIKit       0x000000018e17f630 -[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 3024 (UITextInteractionAssistant.m:1770) 
31 UIKit       0x000000018e59af80 -[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 64 (UIGestureRecognizer.m:103) 
32 UIKit       0x000000018e59e688 _UIGestureRecognizerSendTargetActions + 124 (UIGestureRecognizer.m:986) 
33 UIKit       0x000000018e16573c _UIGestureRecognizerSendActions + 260 (UIGestureRecognizer.m:1020) 
34 UIKit       0x000000018e0040f0 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 764 (UIGestureRecognizer.m:1067) 
35 UIKit       0x000000018e58e680 _UIGestureEnvironmentUpdate + 1100 (UIGestureEnvironment.m:148) 
36 UIKit       0x000000018e58e1e0 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 408 (UIGestureEnvironment.m:1181) 
37 UIKit       0x000000018e58d49c -[UIGestureEnvironment _updateGesturesForEvent:window:] + 268 (UIGestureEnvironment.m:1090) 
38 UIKit       0x000000018e00230c -[UIWindow sendEvent:] + 2960 (UIWindow.m:2289) 
39 UIKit       0x000000018dfd2da0 -[UIApplication sendEvent:] + 340 (UIApplication.m:10729) 
40 UIKit       0x000000018e7bc75c __dispatchPreprocessedEventFromEventQueue + 2736 (UIEventDispatcher.m:1430) 
41 UIKit       0x000000018e7b6130 __handleEventQueue + 784 (UIEventDispatcher.m:1626) 
42 CoreFoundation     0x00000001880cab5c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1943) 
43 CoreFoundation     0x00000001880ca4a4 __CFRunLoopDoSources0 + 524 (CFRunLoop.c:1989) 
44 CoreFoundation     0x00000001880c80a4 __CFRunLoopRun + 804 (CFRunLoop.c:2821) 
45 CoreFoundation     0x0000000187ff62b8 CFRunLoopRunSpecific + 444 (CFRunLoop.c:3113) 
46 GraphicsServices    0x0000000189aaa198 GSEventRunModal + 180 (GSEvent.c:2245) 
47 UIKit       0x000000018e03d7fc -[UIApplication _run] + 684 (UIApplication.m:2650) 
48 UIKit       0x000000018e038534 UIApplicationMain + 208 (UIApplication.m:4092) 
49 Ribony       0x00000001000ce070 main + 120 (AppDelegate.swift:15) 
50 libdyld.dylib     0x0000000186fd95b8 start + 4 

正如你可以看到我在一個try catch塊用這個。它不應該崩潰。這怎麼可能?

+0

你確保numberOfRows不是零,還是負面的? –

+1

你可以設置異常斷點並獲得你的代碼破解的行:http://stackoverflow.com/questions/17802662/exception-breakpoint-in-xcode – viral

回答

0

試試這個代碼

func keyboardWillShow(notification: NSNotification) { 
var info = notification.userInfo! 
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 

self.view.layoutIfNeeded() 
UIView.animateWithDuration(0.2) { 
    self.bottomCons.constant = keyboardFrame.size.height 

    self.view.layoutIfNeeded() 
     } 
    tableViewScrollToBottom(true) 


} 

func tableViewScrollToBottom(animated: Bool) { 

let delay = 0.2 * Double(NSEC_PER_SEC) 
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) 

dispatch_after(time, dispatch_get_main_queue(), { 

    let numberOfSections = self.tableView.numberOfSections() 
    let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1) 

    if numberOfRows > 0 { 
     let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1)) 
     self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated) 
    } 

}) 
}