使用适当的日期设置UIPickerview选择时出现奇怪错误; selectRow导致错误 [英] Odd error when setting UIPickerview selection with appropriate date; selectRow causing error

查看:201
本文介绍了使用适当的日期设置UIPickerview选择时出现奇怪错误; selectRow导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

[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时,它会因此错误而失败:

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).'

它是让我困惑,因为没有与View相关的tableview,所以我有点儿丢失了为什么会发生这种情况。

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.

任何见解?

推荐答案

<事实证明 - 正是UIPickerview正在解决错误。
我手动将一些对象添加到我的一个数组(在组件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.

一旦我确实:

[_editMilesPicker reloadAllComponents]

生活很美好。

这篇关于使用适当的日期设置UIPickerview选择时出现奇怪错误; selectRow导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆