gpt4 book ai didi

ios - 使用适当的日期设置 UIPickerview 选择时出现奇怪的错误; selectRow 导致错误

转载 作者:行者123 更新时间:2023-11-29 03:11:43 27 4
gpt4 key购买 nike

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

这是我的代码:

[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]

生活很美好。

关于ios - 使用适当的日期设置 UIPickerview 选择时出现奇怪的错误; selectRow 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com