繁体   English   中英

将UIPickerview选择设置为适当的日期时出现奇数错误; selectRow导致错误

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

我只在视图中加载一个选择器,一个组件是月份(显示正常),另一个组件显示当前年份(以及前一年和下一年); 如果我注释掉该行以预选择当前月份的值,则选择器显示得很好。

这是我的代码:

[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,所以我对为什么会发生这种情况有些困惑。

有见识吗?

事实证明,正是UIPickerview引发了错误。 我手动将一些对象添加到我的一个数组中(在组件1中使用)-添加这些值后,我需要重新加载组件。

一旦我做了:

[_editMilesPicker reloadAllComponents]

生活是美好的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM