2012-08-24 35 views
4

在移除某些UIScrollView的內容後,調用setContentSize會使應用程序崩潰。UIScrollView setContentSize在未被捕獲的情況下崩潰NSRangeException

int toolbarHeight = [[[self navigationController] toolbar] frame].size.height; 
int navbarHeight = [[[self navigationController] navigationBar] frame].size.height; 

int totalHeight = toolbarHeight + navbarHeight; 
// contentWidth is 640 
CGSize contentSize = CGSizeMake(contentWidth, [scrollView frame].size.height - totalHeight); 

[scrollView setContentSize:contentSize]; // Crash happens here, contentSize is perfectly valid 

*終止應用程序由於未捕獲的異常 'NSRangeException',原因: '* - [__ NSArrayM objectAtIndex:]:索引1超出範圍[0 .. 0]'

這可能是什麼原因造成的?

編輯:這裏的堆棧跟蹤那些誰可以利用它任何意義:

* thread #1: tid = 0x1c03, 0x34d2e32c libsystem_kernel.dylib`__pthread_kill + 8, stop reason = signal SIGABRT 
frame #0: 0x34d2e32c libsystem_kernel.dylib`__pthread_kill + 8 
frame #1: 0x36c2a20e libsystem_c.dylib`pthread_kill + 54 
frame #2: 0x36c2329e libsystem_c.dylib`abort + 94 
frame #3: 0x308eff6a libc++abi.dylib`abort_message + 46 
frame #4: 0x308ed34c libc++abi.dylib`_ZL17default_terminatev + 24 
frame #5: 0x36361356 libobjc.A.dylib`_objc_terminate + 146 
frame #6: 0x308ed3c4 libc++abi.dylib`_ZL19safe_handler_callerPFvvE + 76 
frame #7: 0x308ed450 libc++abi.dylib`std::terminate() + 20 
frame #8: 0x308ee824 libc++abi.dylib`__cxa_rethrow + 88 
frame #9: 0x363612a8 libobjc.A.dylib`objc_exception_rethrow + 12 
frame #10: 0x34e9050c CoreFoundation`CFRunLoopRunSpecific + 404 
frame #11: 0x34e9036c CoreFoundation`CFRunLoopRunInMode + 104 
frame #12: 0x3618c438 GraphicsServices`GSEventRunModal + 136 
frame #13: 0x31f86e7c UIKit`UIApplicationMain + 1080 
frame #14: 0x0000cb50 App`main + 152 at main.m:16 
+0

請解釋你的downvotes!謝謝。 – Gerstmann

+0

我認爲你的問題的解決方案是在你的問題裏面......檢查崩潰時的callstack以及試圖訪問該數組的人以及爲什麼它試圖訪問索引不可用的對象。 – Tutankhamen

+1

@Tutankhamen @Tutankhamen我不瞭解你,但對於我來說,調用堆棧不會說任何東西 – Gerstmann

回答

5

看來,當內容偏移離開了內容的大小外的異常被拋出。

I had views side by side in the scroll view and the offset was to the last view. 

+------+------+------+ 
|  |  |offset| 
+------+------+------+ 

Now the last view is removed and the content size changed to a smaller one. 

+------+------+ 
|  |  |offset 
+------+------+ 

The offset is left behind. 

我改變了代碼,以便首先移動偏移量,然後改變內容大小。

+------+------+ 
|  |offset| 
+------+------+ 

到目前爲止沒有崩潰。

+4

我愛你!!!!!! – Tony

相關問題