2012-02-15 67 views
3

我正在開發基於日曆的應用程序,並且我的客戶需要在用戶選擇的特定日期設置圖像。如何在日曆視圖中設置圖像iPhone

例如,如果我選擇1和20,圖像將在兩個日期上設置。

現在我可以設置圖像僅對於一個日期即,如果我選擇1的圖像被設置在該日期,當我選擇20上1中設定的圖像將消失,圖像將設置僅20.

任何人都可以請幫助我如何做到這一點。

在此先感謝。 enter image description here

+0

檢查此鏈接:http://stackoverflow.com/a/8399779/845115 – 2012-02-15 12:04:00

+0

什麼是你用來顯示日曆,這大部分是一些自定義的,因爲iOS的SDK不提供組件這樣。 – rckoenes 2012-02-15 12:06:08

+0

@MuditBajpai現在我可以設置圖像的一個日期,但需要設置圖像多個日期。請你幫我。 – suji 2012-02-15 12:06:23

回答

1

我終於找到了解決方案。我在該方法中完成了,並且專家可以爲此提出更好的方法。

- (void)selectButtonForDate:(NSDate *)aDate { 



    if (selectedButton >= 0) { 
     NSDate *todayDate = [CalendarLogic dateForToday]; 
     UIButton *button = [buttonsIndex objectAtIndex:selectedButton]; 

     CGRect selectedFrame = button.frame; 
     if ([selectedDate compare:todayDate] != NSOrderedSame) { 
      selectedFrame.origin.y = selectedFrame.origin.y + 1; 
      selectedFrame.size.width = kCalendarDayWidth; 
      selectedFrame.size.height = kCalendarDayHeight; 
     } 

     button.selected = NO; 
     button.frame = selectedFrame; 

     self.selectedButton = -1; 
     self.selectedDate = nil; 
    } 

    if (aDate != nil) { 
     // Save 
     CGRect dayFrame; 
     NSDate *dayDate; 
     NSInteger numberOfWeeks = 5; 
    //  NSCalendar *calendar = [NSCalendar currentCalendar]; 
     // NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]]; 
    //  NSDate *todayDate = [calendar dateFromComponents:components]; 

     for (NSInteger aWeek = 0; aWeek <= numberOfWeeks; aWeek ++) { 
      CGFloat positionY = (aWeek * kCalendarDayHeight) + 60; 

      for (NSInteger aWeekday = 1; aWeekday <= numberOfDaysInWeek; aWeekday ++) { 
       CGFloat positionX = ((aWeekday - 1) * kCalendarDayWidth) - 1; 
       dayFrame = CGRectMake(positionX, positionY, kCalendarDayWidth, kCalendarDayHeight); 
       dayDate = [CalendarLogic dateForWeekday:aWeekday 
               onWeek:aWeek 
              referenceDate:[name referenceDate]]; 
      // NSLog(@" %@ todaydate [datesIndex objectAtIndex:[sender tag] %@",todayDate,[datesIndex objectAtIndex:[sender tag]]); 
       // NSDate *mmm = [datesIndex objectAtIndex:[sender tag]]; 
       if ([dayDate isEqualToDate:aDate]) { 
        NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults]; 

        flag = 1;     
        [n addObject:[NSValue valueWithCGRect:dayFrame]]; 



        [currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:n] forKey:SaveMonth]; 
        [currentDefaults synchronize]; 
      } 



      } 
     } 
     if(flag == 1){ 

      flag = 0; 
      for (int i=0;i<[n count];i++){ 


       UIImageView *im = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"call.png"]] ; 
       CGRect myRect = [[n objectAtIndex:i] CGRectValue]; 
//    NSLog(@"n is %@",n ); 
//    NSLog(@"frame 111111111111111 = %@\n", NSStringFromCGRect(myRect)); 

       im.frame = myRect; 
       [self addSubview:im]; 


      } 
     } 
+0

嗨,我也嘗試做同樣的事情。你在使用KAL庫還是其他的東西? – AtWork 2013-06-07 06:46:09