2012-05-03 4 views
0

lable에 날짜를 인쇄해야합니다. 날짜가 데이터베이스에서 올바르게 표시됩니다. 문제는 데이터베이스 날짜를 현재 날짜와 비교해야합니다. 데이터베이스 날짜가 현재 날짜보다 작 으면 lable은 "Due"라는 문자열을 출력해야합니다. 그렇지 않으면 다음과 같이 날짜를 인쇄하십시오 : 03/05/2012. 난 내의 tableview를 스크롤 할 때 난 내 사용자 정의 셀 수업 시간에 뭐하는 거지 이 코드는 내가 다음 값은 내가 잘못 변화가 나에게아이폰에서 재사용 가능한 셀을 제어하는 ​​방법

- (void) updateContent: (Tasksmodal*) taskobject{ 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
     [dateFormat setDateFormat:@"dd/MM/yyyy"]; 

     NSString* duedate = [dateFormat stringFromDate:taskobject.DueDate]; 


     NSDate *currentdate; 
     currentdate=[NSDate date]; 
     //NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init]; 
     //[dateFormat1 setDateFormat:@"dd/MM/yyyy"]; 
     NSString* duedate1 = [dateFormat stringFromDate:currentdate]; 



    if ([currentdate compare:taskobject.DueDate]!=NSOrderedDescending) { 
[email protected]"Due"; 
DateLable.textColor=[UIColor redColor]; 

}

도와주세요입니다 내가 컨트롤러 클래스를 전달할 방법이 코드를 writen
else{ 
    DateLable.text=duedate; 
    DateLable.textColor=[UIColor blackColor]; 
} 


} 

내 컨트롤러 클래스에 .m // 표보기 셀 모양을 사용자 지정합니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    TasksList * cell =(TasksList*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[TasksList alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    } 
    //[[[cell contentView] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)]; 
    taskobject=(Tasksmodal*)[self.lstTasks objectAtIndex:indexPath.row];  
    [cell updateContent:taskobject]; 

return cell; 

} 

답변

1

이렇게 NSString을 비교할 수 없습니다. 당신은 날짜를 비교하지 않습니다. NSDate 설명서를 확인하십시오. compare을 찾으십시오.

if ([currentdate compare:taskobject.DueDate]!=NSOrderedDescending) { 
     [email protected]"Due"; 
     DateLable.textColor=[UIColor redColor]; 
    } 
    else{ 
     DateLable.text=duedate; 
     DateLable.textColor=[UIColor blackColor]; 
    } 


코드는 또한 NSDateFormatter의 유출된다. 하나의 NSDateFormatter을 사용하여 2 개의 날짜를 형식화 할 수 있으며 새 날짜를 할당 할 필요가 없습니다.

+1

Rocky가 'ARC'를 사용하고 있다면 누출되지는 않지만 dateformatter를 재사용하는 것이 좋습니다. – rckoenes

+0

ssteinberg하지만 내 테이블 셀을 스크롤 할 때 셀이 재사용되고 있습니다. 그러면 가치가 예정대로 변경됩니다. 03/05/2012 이유가 무엇입니까? – Rocky

+0

셀 관리가 잘못되어 나머지 방법을 게시합니다. – ssteinberg

0

duedate의 메모리 주소가 모두 포인터 유형이므로 duedate1의 메모리 주소보다 작은 지 비교하면 (duedate<=duedate1)이 확인됩니다. 이것은 아마도 당신이 정말로 원하는 것이 아닙니다 :-). 대신 다음과 같이 시도하십시오.

if ([taskobject.DueDate timeIntervalSinceNow] <= 0) { 
    ... due 
} 
else { 
    ... not due 
} 
0

NSComparisonResult result = [taskDject.DueDate compare : [NSDate date]];

if(result>0) 
    [email protected]"Due"; 
else 
    DateLable.text=duedate; 
0

NSDate 객체를 직접 비교할 수 있습니다. 실제로 수행하는 것보다 빠르고 효율적으로 접근 할 수 있습니다.

관련 문제