Odd error when setting UIPickerview selection with appropriate date; selectRow causing error

瘦欲@ 提交于 2019-12-11 18:13:23

问题


I'm loading just 1 picker in a view, one component is months (which displays fine), the other displays the current year (as well as previous and next years); The picker displays just fine if I comment out the line to pre-select the value for the current month.

Here is my code:

[super viewDidLoad];
// Do any additional setup after loading the view.

NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc]init];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone localTimeZone]];

//Month Array of all months
monthArray = [df monthSymbols];
int i = 0;
while (i < [monthArray count]) {
NSLog(@"month array: %@", [monthArray objectAtIndex:i]);
    i++;
}
//Get Current Month
[df setDateFormat:@"MMMM"];
NSString *currentMonth = [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
NSLog(@"current month: %@", currentMonth);
int currentMonthIndex = [monthArray indexOfObject:currentMonth];
NSLog(@"Current month index: %d",currentMonthIndex);
[_editMilesPicker selectRow:currentMonthIndex inComponent:0 animated:YES]; //THIS IS THE LINE THAT CAUSES THE ERROR - IF I COMMENT IT OUT THE VIEW DISPLAYS PERFECTLY FINE AND THE PICKER SHOWS THE LIST OF MONTHS

The monthArray prints out fine. The currentMonth prints appropriately. It finds the correct index in the monthArray - but when I try to get it to selectRow it fails with this error:

2014-03-04 14:53:38.709 54 Miles[26382:70b] *** Assertion failure in -[UIPickerTableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2903.23/UITableView.m:1330
2014-03-04 14:53:38.716 54 Miles[26382:70b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

It's got me confused because there is no tableview associated with the View, so I'm a little lost as to why this might be happening.

Any insight?


回答1:


As it turns out - it was the UIPickerview that was kicking the error off. I was manually adding some objects to one of my arrays (used in component 1) - I needed to reload the components after adding those values.

Once I did:

[_editMilesPicker reloadAllComponents]

Life was good.



来源:https://stackoverflow.com/questions/22183025/odd-error-when-setting-uipickerview-selection-with-appropriate-date-selectrow-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!