Reputation: 27
I have implemented an universal app that can run on all iOS devices. Recently, I encountered a weird problem which my app will go fail on the iPhone simulator, but will go smooth on the iPad simulator.
I have found out which part in my program had bugs, but I had no idea to fix it. In the AppDelegate, I have this code:
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];
}
The app load data,called vocabulary_ from remote database fulfilled by JSON into my iPhone_ASRAViewContriller's NSArray property, called listData, and then show it on the tableview.
In order to concatenate vocabulary showed in the table, I have code as following:
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);
When I simulate the app on the iphone simulator, there are some error message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
The app seems not to concatenate the string in "table" under the iPhone simulator. Can anyone provide me with a suggestion?
The following code are my implementation of 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];
Upvotes: 1
Views: 245
Reputation: 90117
Your problem is this piece of code:
[cells addObject:[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
especially the part were you try to get a cell from the tableView, cellForRowAtIndexPath:
.
This method does not guarantee that a cell will be returned. If a cell is not visible this method will return nil.
This probably works on iPad because due to the larger screen size all your cells are visible. On iPhone not all cells are visible at the same time so cellForRowAtIndexPath:
will return nil for some of your indexPaths.
Get your data from the dataSource, not from the tableView. The tableView is a view, it does not store data.
Upvotes: 3