簡體   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