2012-12-05 64 views
1

enter image description here如何在文檔目錄中保存當前日期和時間值?

正如我上面的屏幕截圖顯示,我想在泰伯維的每個單元顯示兩個值。我可以做到這一點,如果我有我想要在同一個類的Cell的這些標籤中顯示的數據,但問題是我試圖從其他視圖或其他類使用(NSDocumentDirectory,NSUserDomainMask,YES )因此,我成功地顯示了一個值作爲我的截圖顯示,但由當前數據和時間顯示組成的其他部分仍然是我的問題。現在這裏是我的代碼,我嘗試這樣做。

NSString *fina = [NSString stringWithFormat:@"%@",mySongname]; 
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docsDir = [dirPaths objectAtIndex:0]; 
    NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"MyRecordings"]; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:soundFilePath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:soundFilePath withIntermediateDirectories:NO attributes:nil error:nil]; 
    soundFilePath = [soundFilePath stringByAppendingPathComponent:fina]; 
    recordedTmpFile = [NSURL fileURLWithPath:soundFilePath]; 
    recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error]; 
    [recorder setDelegate:self]; 
    [recorder prepareToRecord]; 
    [recorder record]; 
    [recordSetting release]; 

代碼的上述部分工作正常,它顯示泰伯維細胞,我在同樣的功能screenshow.Now我試圖獲取數據來顯示它在紅​​色部分是用我的泰伯維細胞的UILabel值代碼如下。

NSDate* now = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd:MMM:YY_hh:mm:ss a"]; 
NSString *file= [dateFormatter stringFromDate:now]; 
NSLog(@"myfinaldate:%@",file); 

現在我想保存我在上面使用,並顯示它的UITableView的紅色部分相同的目錄此日期值。 現在這裏是我的代碼,我使用此Tableview並獲取這些文檔目錄值。

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *documentPath = [documentsDirectory stringByAppendingPathComponent:@"MyRecordings"]; 
directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath:documentPath]; 
     NSLog(@"file found %i",[directoryContent count]); 
[directoryContent retain]; 
[self.tableView reloadData]; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 
static NSInteger StateTag = 1; 
static NSInteger CapitalTag = 2; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    UILabel *capitalLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, 80, 20)]; 
    //[email protected]"mydata"; 
    capitalLabel.backgroundColor=[UIColor redColor]; 
    capitalLabel.tag = CapitalTag; 
    [cell.contentView addSubview:capitalLabel]; 
    [capitalLabel release]; 

    UILabel *stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 22, 310, 20)]; 
    stateLabel.tag = StateTag; 
    [stateLabel setFont:[UIFont systemFontOfSize:14]]; 
    stateLabel.adjustsFontSizeToFitWidth=YES; 
    [cell.contentView addSubview:stateLabel]; 
    [stateLabel release]; 

} 
UILabel * stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag]; 
//UILabel * capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag]; 

stateLabel.text = [directoryContent objectAtIndex:indexPath.row]; 
//capitalLabel.text = [directoryContent1 objectAtIndex:indexPath.row]; 
    return cell; 
} 

現在我正試圖總結這個問題。 我們如何將日期和時間值保存在同一目錄中,然後如何在UITableview Cell的紅色部分顯示? capitalLabel是我單元格的Redpart顯示日期和時間的問題。所有的狀態標籤都準備好顯示值。所以這個標籤沒有問題。任何幫助將不勝感激。

回答

0

只是這樣做....

可以設置NSCachesDirectoryNSDocumentDirectory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:yourComponent]; 
// NSLog(@"Path : %@",writableDBPath); 

NSMutableDictionary *plistArr = [[NSMutableDictionary alloc] initWithContentsOfFile:writableDBPath]; 
+0

節約值,那麼我們怎樣才能把它拿來之後。 – NSCool

相關問題