2013-04-18 42 views
4

我在不同的設備上測試我的應用程序。該應用程序適用於我的iPhone 5的罰款,但是,當我測試它在我的iPod touch 4G,我得到這個eror:代碼工作在iPhone 5上,不在iPod touch 4上

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** [__NSArrayMinsertObject:atIndex:]: object cannot be nil' 
*** First throw call stack: 
(0x33f252a3 0x3bba397f 0x33e6f8d9 0x51e25 0x35e180c5 0x35e1814d 0x35e180c5 0x35e18077 0x35e18055 0x35e1790b 0x35e17e01 0x35d405f1 0x35d2d801 0x35d2d11b 0x37a1f5a3 0x37a1f1d3 0x33efa173 0x33efa117 0x33ef8f99 0x33e6bebd 0x33e6bd49 0x37a1e2eb 0x35d81301 0x4fc8d 0x3bfdab20) 
libc++abi.dylib: terminate called throwing an exception 

這都崩潰的代碼,這是一個:

/********/ 
    NSMutableArray *titulosCampoTextArray = [[NSMutableArray alloc] init]; 
    for (int i = 0; i < campos.count; i++) { 

     SignupCell *cell = (SignupCell*)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     [titulosCampoTextArray addObject:cell.textField.text]; 
    } 
/********/ 

我m使用自定義原型單元格(SignupCell.h),在UIView內的UITableView上。

找不到錯誤,或者如何解決。

編輯:這兩個設備都運行iOS 6.1.3,我使用Xcode 4.6.1。

+0

2個設備的iOS版本是什麼? – Raptor 2013-04-18 03:04:09

+0

@ShivanRaptor iOS 6.1.3在這兩個設備上。 – 2013-04-18 03:08:24

回答

5

UITableView-cellForRowAtIndexPath:方法返回nil運行,如果該細胞是在屏幕上不可見時你叫它。

我的猜測是,在iPhone 5上,您的桌面可以一次顯示所有行,而在較短的iPod touch上,最後一個或兩個單元格不可見,因此-cellForRowAtIndexPath:返回nil,這會使您的應用崩潰。

您不應該依賴-cellForRowAtIndexPath來請求模型數據!您的視圖控制器負責數據,而不是表格視圖。

使用相同的方式訪問模型數據,就像您在視圖控制器的-tableView:cellForRowAtIndexPath:數據源方法中設置表格視圖單元格的文本一樣。

+1

+1 Sherlock Holmes – MusiGenesis 2013-04-18 11:36:45

+0

同意@MusiGenesis。實際上發生了什麼事情。非常感謝。 – 2013-04-18 19:23:15

1

只是這樣做:

NSMutableArray *titulosCampoTextArray = [[NSMutableArray alloc] init]; 
for (int i = 0; i < campos.count; i++) { 

    SignupCell *cell = (SignupCell*)[self.tableView 
     cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
    if (cell.textField.text) { 
     [titulosCampoTextArray addObject:cell.textField.text]; 
    } 
} 

我不知道爲什麼你顯然可以在iPod上的iPhone 5添加nil但不是。這可能是因爲一些其他原因你只是不碰巧有一個nil價值cell.textField.text當您的應用程序在iPhone 5

+0

事實上,我在測試時在兩臺設備上以相同的順序填充表格,但仍然出現此錯誤。 – 2013-04-18 03:16:22

+2

看到我爲什麼'-cellForRowAtIndexPath:'可能返回零的答案。 – 2013-04-18 06:33:59

相關問題