2017-04-05 68 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