2011-01-06 7 views
0

uipicker를 채우는 데 사용할 수있는 코드를 찾고 있습니다. 여기에는 두 가지 구성 요소가 있습니다. 첫 번째 구성 요소는 DAY 및 DATE가 필요합니다. 두 번째 구성 요소는 처리 할 수 ​​있습니다.UIPicker에 현재 요일 + 날짜를 채우는 방법

수요일 20 번째 | 오후 2시 |
| 목요일 21 번째 | 오후 1시 |
기타

두 번째 구성 요소를 처리 할 수 ​​있습니다. 첫 번째 구성 요소의 서식을 지정하기위한 샘플 코드를 찾을 수 없습니다. 먼저 오늘 날짜를 결정하고 그 다음에 날짜를 결정해야한다는 것을 이해합니다. 그런 다음 거기에서 빌드하지만 누군가가 이와 비슷한 몇 가지 샘플 코드를 갖기를 희망했습니다. 도움을 주시면 대단히 감사하겠습니다.

+0

나는 내 자신의 질문에 대한 답변을 여기에 게시 된 코드 않습니다. 그 추한 그러나 그것은 일한다!! – Louie

+0

전체 달 또는 주를 원합니다. – Srinivas

답변

3

다른 방법이나이 일을 더 나은 방법을 찾는 부족합니다. 나는 이것을 함께 쓴다. 필자는 원하는대로 형식화 된 날짜 배열을 작성합니다. 그것의 예쁜 아니지만 나는 그것을 할 수있는 더 좋은 방법을 찾을 수 없습니다. FOR 회 돌이가 포맷 된 날짜의 마지막 2자를 더 쉽게 설정할 수 있다고 생각할 것입니다.하지만 그 기묘함이있는 것으로 판명되었으므로 나는 그것을 수정하려고 한 시간을 보내고 싶지 않습니다. 그래서 쉽게 못생긴 방법을 벗어났습니다. 오늘 날짜와 요일을 시작으로
수요일 5
목요일 6
금요일 7
등 ...
:

아래 코드는 이와 같은 일 & 날짜의 형식의 배열을 생성합니다.

- (void)createDatesForPicker { 


    daysForPicker = [[NSMutableArray alloc] initWithCapacity:0]; 
    formatedDaysForPicker = [[NSMutableArray alloc] initWithCapacity:0]; 

    //get todays Day & Date 
    NSDate *today1 = [NSDate date]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"EEEE dd"]; 

    //change how many days out you want to go here I am going 14 days out 
    for (int i=0; i<14; i++){ 

     NSCalendar *gregorian11 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

     NSDateComponents *components1 = [gregorian11 components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today1]; 
     [components1 setDay:([components1 day]+i)]; 

     NSDate *beginningOfWeek1 = [gregorian11 dateFromComponents:components1]; 
     NSDateFormatter *dateFormat_weekend = [[NSDateFormatter alloc] init]; 
     [dateFormat_weekend setDateFormat:@"EEEE dd"]; 
     NSString *dateString_first = [dateFormat_weekend stringFromDate:beginningOfWeek1]; 

     // unformatted dates array 
     [daysForPicker addObject:dateString_first]; 

    } 

    // now we have all the days and date, so lets format them so they look good 
    for (int i=0; i<[daysForPicker count];i++) { 
     NSString * currentDay = [NSString stringWithFormat:@"%@",[daysForPicker objectAtIndex:i]]; 

     //we have date now look at 2nd to last digit and check if it is a zero, if so remove it. 
     NSUInteger count = [currentDay length]-1; 
     NSRange r = NSMakeRange(count,1); 
     NSString * lastChar = [currentDay substringWithRange:r]; 



     if ([lastChar isEqual:@"0"] == TRUE) { 
      //replace it with nothing 
     } 
     else { 
      //not a zero at the end so we are good to remove all zeros 
      currentDay = [currentDay stringByReplacingOccurrencesOfString:@"0" withString:@""]; 
     } 

     //used later for date formatting 
     NSUInteger count2 = [currentDay length]-2; 
     NSRange r2 = NSMakeRange(count2,2); 
     NSString * dateDigits = [currentDay substringWithRange:r2]; 

     if ([dateDigits isEqual:@" 1"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"st"]; 
     } 
     if ([dateDigits isEqual:@" 2"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"nd"]; 
     } 
     if ([dateDigits isEqual:@" 3"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"rd"]; 
     } 
     if ([dateDigits isEqual:@" 4"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@" 5"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@" 6"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@" 7"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@" 8"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@" 9"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"10"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"11"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"12"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"13"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"14"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"15"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"16"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"17"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"18"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"19"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"20"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"21"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"st"]; 
     } 
     if ([dateDigits isEqual:@"22"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"nd"]; 
     } 
     if ([dateDigits isEqual:@"23"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"rd"]; 
     } 
     if ([dateDigits isEqual:@"24"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"25"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"26"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"27"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"28"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"29"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"30"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"th"]; 
     } 
     if ([dateDigits isEqual:@"31"] == TRUE) { 
      currentDay = [currentDay stringByAppendingString:@"st"]; 
     } 


     // add formated string to array 
     [formatedDaysForPicker addObject:currentDay]; 

    } 



} 
+0

보다 쉬운 정규화를 위해 ghkit의 gh_ordinalizeEn 메소드를 확인하십시오. https://github.com/gabriel/gh-kit/blob/master/Classes/GHNSNumber+Utils.m – johnboiles

+0

멋진 답변. – Legolas

+0

다행이라면 유용하다는 것을 알았 기니 – Louie

1

수행하면 당신에게 일 이름과 일 날짜 줄 것이다 :

 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"EEEE d"]; // name of the day and day number 
dateString = [self.dateFormatter stringFromDate: aDate]; // Monday 20 
[dateFormatter release]; 
+0

감사합니다. 나는 이것을 더 일찍 찾았고, 내가하려고했던 것의 포맷팅 작업에 1 시간을 보내고있다. 다른 사람들이 사용할 수 있도록 게시했습니다. – Louie

관련 문제