2017-04-05 22 views
0

在我的iOS应用程序项目中,我创建了保存在文档目录文件结构中的录制的audio.caf文件,然后将其显示为数组,列在TableView的连续TableViewCells中。 我想要在每个单元格中连续显示作为标题 和副标题创建的文件的唯一“音频文件名”和“NSDate”。 用于在给定路径创建记录文件的代码位于Mainviewcontroller中。显示文件名和NSDateString作为TableView标题(需要帮助)的唯一文件名

NSArray *pathComponents = [NSArray arrayWithObjects: 
            [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], 
            _filePath, 
            nil]; 
     newURL = [NSURL fileURLWithPathComponents: pathComponents]; 
     _filePath = [documentsDirectory stringByAppendingPathComponent:@"audio %@ %@.caf"]; 
     // Initiate and prepare the recorder 
     //NSInteger count = 0; 
// To create the date string <--- 
     NSString *dateString; 
     NSDate *aDate = [NSDate date]; 
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
     dateString = [dateFormatter stringFromDate:aDate]; 
     NSLog(@"datestring %@",dateString); 


     int count = 0; 
     int arrayCount = [audioFileArray count]; 



      documentsDirectory = [pathComponents objectAtIndex:0]; 

      NSError *error = nil; 
// To create the recorded file name and date combined <--- 
      _audioFileArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error]; 
      filename = [NSString stringWithFormat:@"audio %@ %@.caf", dateString,[NSNumber numberWithFloat:[_audioFileArray count]]]; 
        count ++; 

我也希望这样的文件“数字”来了“的NSDate”之前,但是这是它的工作原理,即只有这样,才能重新安排代码; _audioFileArray计数位之前的DateString!

并且,在的UITableView fileViewController显示文件名的文本的位是:

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

static NSString *simpleTableIdentifier = @"simpleTableItem"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:simpleTableIdentifier]; 
} 
NSString *name = [_filteredArray objectAtIndex:indexPath.row]; 
//add the text display that is drawn from the array list 
cell.textLabel.text = name; 
cell.textLabel.text = [_filteredArray objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ is the recorded date", name]; 
return cell 

我的阵列,在UITableViewCells显示的标题在该图示被示出。

Table view cells comparison

我所希望做的是去除在每个单元的标题的日期(创建)部分和重新定位,在detailtextLabel标题来代替。 我可以做到这一点可能是通过创建一个单独的NSDate类,它可以被主视图控制器和详细视图控制器 同时调用(或利用),但我仍然没有成功尝试这种方法。

例如,如果使用JSON或plist为每个保存的文件属性创建一个字典,这可以更好地工作。 任何与此有关的帮助将不胜感激,如果这篇文章需要更清晰,请让我也知道。

最后,我在论坛上搜索了很长时间,对这个问题有一个简明的解决方案,至今还没有找到解决办法。

非常感谢。

回答

0

正如我看到你的代码,似乎日期字符串返回零或空值。与此代码

NSString *dateString; 
NSDate *aDate = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
dateString = [dateFormatter stringFromDate:aDate]; 
NSLog(@"datestring %@",dateString); 

: 更换你的代码

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"dd-MM-yyyy"]; 
NSDate *currentDate = [NSDate date]; 
NSString *dateString = [formatter stringFromDate:currentDate]; 
NSLog(@"datestring %@",dateString); 

谢谢。

+0

ok..let我知道。它的工作与否。 – Sommm

+0

@Somme。回到你以前的提交。我在我的示例中一起入侵的NSDate格式化程序实际上会返回一个日期字符串。也就是说,当它位于Mainviewcontroller和音频文件创建类的范围内时。然而,当我将它从这个主文件中移出并作为一个单独的NSDate类时,这就是我在调用它时遇到问题的地方。 – richg

+0

因此,现在它只是在这个视图控制器中执行一项服务。 我的主要问题是我需要解析文件名的字符串(保存有唯一的文件序列号和格式化日期),并在表 中查看单元格作为主标题和详细文本字幕,显示格式化日期。 (请查看我上面的图像链接,以显示它需要如何工作) 再一次,如果能够成功创建所有ViewController可以调用的NSDate类/文件,这甚至可以让我获得一些成功。 (我希望这篇文章不太含糊) 任何反馈赞赏 – richg