我在一个视图中只加载了一个选择器,一个组件是几个月(显示正常),另一个组件显示当前年份(以及前一年和下一年);如果我注释掉该行以预先选择当月的值,则选择器显示正常。
这是我的代码:
[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
monthArray打印得很好。 currentMonth打印得恰当。它在monthArray中找到了正确的索引 - 但是当我尝试将它设置为selectRow时,它失败并出现此错误:
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).'
这让我感到困惑,因为没有与View相关的tableview,所以我有点迷失为什么会发生这种情况。
有什么见解?
答案 0 :(得分:1)
事实证明 - 正是UIPickerview正在解决错误。 我手动将一些对象添加到我的一个数组(在组件1中使用) - 我需要在添加这些值后重新加载组件。
我做过:
[_editMilesPicker reloadAllComponents]
生活很美好。