2015-06-23 20 views
0

我想將我的JTCalendarkJTCalendarDaySelected設置爲等於可見的index.section。如果索引爲0,我希望kJTCalendarDaySelected設置爲1(本月的第一個月),如果索引爲1,我希望它設置爲2(本月的第2個月),依此類推。如何使用UITableView節索引作爲NSInteger?

當我嘗試這樣做,像這樣:

-(BOOL)whatSectionsAreVisible { 
    NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows]; 
    for (NSIndexPath *index in visibleRowIndexes) { 

     self.daySection = index.section; 

     // Update views 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"kJTCalendarDaySelected" object:self.daySection]; 

     // Store currentDateSelected 
     [self.calendar setCurrentDateSelected:self.daySection]; 

     NSLog(@"The visible section has an index of: %ld", (long)index.section); 

     if (index.section == 0) { 
      NSLog(@"index.row is indeed 0"); 
      return YES; 
     } 
    } 
    return NO; 
} 

我得到以下錯誤:

implicit conversion of non-Objective-c pointer type 'NSInteger' (aka 'long') to 'id' is disallowed with ARC enter image description here

我怎麼能轉換index.section的id設置爲一個NSInteger,可以用於這個目的?

+1

使用'NSNumber * indexNumber = [NSNumber numberWithInteger:self.daySection]'並在通知對象中傳遞'indexNumber'。你可以通過'NSInteger daySectionIndex = [indexNumber integerValue]'來將它轉換爲整數。 – Akhilrajtr

+0

要回答標題中的問題:節索引***是***'NSInteger',並且***不是指向對象的指針。 'postNotification'方法需要一個指針,顯然你的'setCurrentDateSelected'方法也需要一個指針。你的問題不清楚的是通知觀察者的對象類型和'setCurrentDateSelected'方法期望的類型。 – user3386109

+0

...或者你可以使用現代拳擊語法:'... object:@(self.daySection)' –

回答

0

您可以使用下面的代碼,刪除你的錯誤

[[NSNotificationCenter defaultCenter] postNotificationName:@"kJTCalendarDaySelected" object:[NSNumber numberWithInteger:self.daySection]]; 

NSString *strDate = [NSString stringWithFormat:@"%@", [NSNumber numberWithInteger:section]]; 

NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
[formatter setDateFormat:@"MM"]; 
[formatter setDateStyle:NSDateFormatterMediumStyle]; 

NSDate *selecteDate = [[formatter standaloneMonthSymbols] objectAtIndex:[formatter dateFromString:strDate]]; 

NSLog(@"%@",selectedDate); 

這裏selectedDate將在月份名稱的形式,如果你的indexpath.section是「0」,那麼它會給你'1月',所以你可以在日曆中設置月份名稱。

0

您需要傳遞參數類型ID。 假設你可以以這種方式使用..

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:self.daySection],@"sectionIndex", nil]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"kJTCalendarDaySelected" object:dict]; 

希望這將幫助你...