2012-12-05 30 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

相关问题