2013-07-12 3 views
0

다운로드 관리자가 내장 된 앱이 있습니다. 파일을 다운로드하면 문서 폴더 안의 폴더에 저장됩니다. 이제 테이블 뷰 셀에 시간 및 날짜 스탬프를 구현했습니다. 그러나 한 뷰 컨트롤러에서 내 테이블 뷰로 다시 전환하면 시간과 날짜가 현재 시간으로 업데이트됩니다.타임 스탬프가 계속 계산됩니다.

아래 코드는 제가 작업하고있는 코드이며 항상 도움이 될 것입니다.

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } //did the subtitle style 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [directoryContents objectAtIndex:row]; 


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"EEE, MMM/d/yyyy, hh:mm aaa"]; 
    NSDate *date = nil; 

    if (indexPath.row == 0) 
    { 
     date = [NSDate date]; 
     NSString *dateString = [dateFormatter stringFromDate:date]; 
     cell.detailTextLabel.text = dateString; 
    } 

    return cell; 
} 

답변

0

다시 동그라미로 결정했습니다. 나는 그것을 알아내는 것을 끝내었다 (내가이 전체의 시간 lol에 그것에 맞붙고 있었던 것은 아니다).

은 어쨌든을 heres 내가 무슨 짓을했는지 :

//Setting the date 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"my folder"]; 
    filePath = [filePath stringByAppendingPathComponent:fileName]; 

    NSDate *creationDate = nil; 
    NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil]; 
    creationDate = attributes[NSFileCreationDate]; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MM-dd-yyyy"]; 
    NSString *dateString = [dateFormatter stringFromDate:creationDate]; 

    cell.detailTextLabel.text = dateString; 

은 누군가가 도움이되기를 바랍니다.

2

tableView:cellForRowAtIndexPath:는 테이블이 다시로드 될 때마다 호출됩니다, 따라서 시간이 [NSDate date]가 다시 호출되기 때문에 현재 시간으로 업데이트됩니다.

tableView:cellForRowAtIndexPath:으로 날짜를 계산하는 대신 다른 곳 (예 : 클래스 수준 NSMutableArray)에 저장하고 파일 다운로드가 완료되면 설정해야합니다. 이렇게하면 테이블이로드 될 때마다 다시 설정되지 않습니다.

+0

+1 좋은 해결책 제안 –

+0

내 다운로드 관리자에서 설정 한 경우 내 테이블보기에 어떻게 표시 되나요? 도움이된다면 HCDownload를 사용하여 파일을 다운로드하고 있습니다. – ChrisOSX