Search code examples
iosobjective-cuitableviewnsarray

Displaying unique file name with File name and NSDateString as TableView titles (need help)


In my iOS app project, I create recorded audio.caf files saved in the documents directory file structure, which are then to be displayed as an array, listed in consecutive TableViewCells of the TableView. I want show both the unique “audio file name” and “NSDate” of the files created as a title and subtitle consecutively , within each cell. The code used to create the recorded file at a given path is in the Mainviewcontroller.

    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 ++;

I also want to re-arrange the code so the file "number" comes before the "NSDate", but this is the only way that it works ie; DateString before the _audioFileArray count bit!

And, in the UITableView fileViewController the bit that displays the filenames text is:

    - (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

The titles of my array, displayed in the UITableViewCells are shown in this illustration.

Table view cells comparison

What I am looking to do is remove the date (created) part of the title in each cell and relocate that in the detailtextLabel title instead. I could do this maybe by creating a separate NSDate class that can be called (or utilised) by the main view controller and the detail view controller simultaneously, but I however haven’t been successful in trying this approach yet.

for instance, would this work better instead by using a JSON or plist to create a dictionary for each saved files properties. Any help with this would be greatly appreciated, and if this post needs more clarity, please let me know also.

Lastly, I have searched long and wide on the forums for a concise solution to this problem, and have not found a fix, as yet.

Many thanks.


Solution

  • As i saw your code, it seems that the date string return nil or empty value. replace your code:

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

    with this code:

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

    Thank you..