1

我正在录制应用程序。在这个应用程序中,我用我自己的文本保存我的录音,它还用当前日期和时间保存录音。如下面的代码所示

-(IBAction)RecButtonPress:(id)sender
{
    NSLog(@"Song name:%@",mySongname);
    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:44000.0] forKey:AVSampleRateKey]; 
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
    [recordSetting setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
    [recordSetting setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
    [recordSetting setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
    NSDate* now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd:MMM:YY_hh:mm:ss a"];
    NSString *file= [dateFormatter stringFromDate:now];
    NSString *fina=[file stringByAppendingString: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];
    NSLog(@"Uf:%@",recordedTmpFile);
    recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
    [recorder setDelegate:self];
    [recorder prepareToRecord];
    [recorder record];
        [recordSetting release];
       [dateFormatter release];
}

现在保存记录后,当我转到 SaveRecording 类时,实际上我在 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]);
NSLog(@"arraydata: %@", directoryContent );
[directoryContent retain];
[self.tableView reloadData];
 }

之后,我将我的 NSMutablArray 的“directoryContent”分配给 UITableview。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [directoryContent count];
}

///////////////////////////////////////// ////////////////////////////////////

- (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, 120, 20)];
    //capitalLabel.text=@"mydata";
    capitalLabel.backgroundColor=[UIColor redColor];
    capitalLabel.tag = CapitalTag;
    [capitalLabel setFont:[UIFont systemFontOfSize:9]];
    [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 = [datesaving objectAtIndex:indexPath.row];

return cell;
}

最后我的 UITableView 在屏幕截图下面看起来像这样 在此处输入图像描述

我所有这些简短讨论的目的是,当我的屏幕截图显示 UITableview 单元格显示我的文本和当前日期和时间。现在我想将此目录内容数组数据拆分为两个 prats。我想要的包含当前日期和时间的部分将它分配给 UITableview 中单元格的红色部分的大写标签,并将文本分配给位于 UITableview 中单元格部分下方的 stateLabel。任何帮助都将得到应用。提前致谢。

4

2 回答 2

1

嗨,莉娜,您可以尝试以下方法

1) 字典数组

将您的日期和歌曲名称分别保存在字典中

 NSDictionary *myData    =   [NSDictionary dictionaryWithObjectsAndKeys:myDateObj,@"SongDate",mySongName,@"SongName", nil];

[myMutableArray addObject:myData]//myMutableArray is a NSMutableArray;

现在您可以按如下方式使用它

NSDictionary *dict = [directoryContent objectAtIndex:indexPath.row];
stateLabel.text =    [dict objectForKey:@"SongName"];
capitalLabel.text =  [dict objectForKey:@"SongDate"];

或者

2)你可以做一个小技巧:)

NSString *fina=[file stringByAppendingFormat:@"+%@",mySongname];

NSArray *Array  =   [fina componentsSeparatedByString:@"+"];

capitalLabel.text =  [Array objectAtIndex:0];

stateLabel.text =    [Array objectAtIndex:1];

在这里追加时,您可以使用格式并添加任何特殊字符,以后可以使用这些字符来拆分字符串。

希望这会以任何方式帮助你:)

于 2012-12-06T09:32:21.293 回答
1

首先你需要像这样附加你的字符串

NSString *file3 = [file stringByAppendingString:@"+"];


NSString *fina= [file3 stringByAppendingString:mySongname];

之后你需要把它分开

NSArray *Array = [str componentsSeparatedByString:@"+"]; 
NSLog(@"myindex0str:%@",[Array objectAtIndex:0]); 
NSLog(@"myindex1str:%@",[Array objectAtIndex:1]);

因此您将分别获得时间和歌曲名称。

于 2012-12-06T12:45:47.083 回答