[英]App with UITableView runs on iPad simulator but crashes on the iPhone simulator
我已經實現了一個可以在所有iOS設備上運行的通用應用程序。 最近,我遇到了一個奇怪的問題,我的應用程序將在iPhone模擬器上失敗,但將在iPad模擬器上順利進行。
我發現程序中哪個部分有錯誤,但我不知道修復它。 在AppDelegate中,我有這個代碼:
id someController=[self.tabBarController.viewControllers objectAtIndex:3];
if ([someController isKindOfClass:[UINavigationController class]]){
someController = [someController topViewController];
}
if ([someController isKindOfClass:[iPhone_ASRAViewController class]]) {
iPhone_ASRAViewController *myIPhone_ASRAViewController=(iPhone_ASRAViewController*)someController;
myIPhone_ASRAViewController.listData=[NSArray arrayWithArray:vocabulary_];
[myIPhone_ASRAViewController.table reloadData];
}
應用程序從遠程數據庫加載數據,稱為vocabulary_,由JSON實現到我的iPhone_ASRAViewContriller的NSArray屬性中,稱為listData,然后在tableview上顯示它。
為了連接表中顯示的詞匯,我的代碼如下:
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [table numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [table numberOfRowsInSection:j]; ++i)
{
[cells addObject:[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
NSString *postmsg=@"SA_VC=0&select_language=english&txtFilePath=";
for (UITableViewCell *cell in cells)
{
NSString *temp=[postmsg stringByAppendingString:cell.textLabel.text];
postmsg=[temp stringByAppendingString:@"\r\n"];
}
NSString *final_postmsg=[postmsg stringByAppendingString:@"&waveBase64=%@"];
NSLog(@"%@",final_postmsg);
當我在iphone模擬器上模擬應用程序時,會出現一些錯誤消息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
該應用程序似乎不會連接iPhone模擬器下的“表”中的字符串。 任何人都可以給我一個建議嗎?
以下代碼是我對tableView的實現:cellForRowAtIndexPath:
static NSString *TableIdentifier = @"tableidentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:TableIdentifier] autorelease];
NSDictionary *voc_list=[listData objectAtIndex:indexPath.row];
NSLog(@"%@",voc_list);
cell.textLabel.text = [[(NSDictionary*)voc_list objectForKey:@"vocabulary_list"]objectForKey:@"Vocabulary"];
cell.detailTextLabel.text=[[(NSDictionary*)voc_list objectForKey:@"vocabulary_list"]objectForKey:@"Translation"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
你的問題是這段代碼:
[cells addObject:[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
特別是你嘗試從tableView, cellForRowAtIndexPath:
獲取一個單元格。
此方法不保證將返回單元格。 如果單元格不可見,則此方法將返回nil。
這可能適用於iPad,因為屏幕尺寸較大,所有單元格都可見。 在iPhone上,並非所有單元格同時可見,因此對於某些indexPath, cellForRowAtIndexPath:
將返回nil。
從dataSource獲取數據,而不是從tableView獲取數據。 tableView是一個視圖,它不存儲數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.