如何将NSMutableArray数据拆分为两部分?

时间:2012-12-06 07:39:42

标签: iphone xcode uitableview directory nsmutablearray

我正在处理录制应用程序。在此应用程序中,我使用自己的文本保存录制内容,并且还保存当前日期和时间的录制内容。如下面的代码显示

-(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];
 }

之后我分配“directoryContent”这是我的NSMutablArray到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看起来像是在ScreenShot下面 enter image description here

我的所有这个简短的讨论目的是,当我的屏幕截图显示UITableview单元格显示我的文本和当前日期和时间。现在我想将此目录内容数组数据拆分为两个prats.The包含当前日期和时间的部分我想将它分配给CapitalLabel,它是UITableview和Text中的Cell的redpart到stateLabel,它是UITableview中Cell的一部分。任何帮助都将被Appriated.Thanks in Advance。

2 个答案:

答案 0 :(得分:1)

你好Lena你可以尝试以下事情

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"];

OR

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

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

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

capitalLabel.text =  [Array objectAtIndex:0];

stateLabel.text =    [Array objectAtIndex:1];

这里附加你可以使用格式并添加任何特殊字符,以后可以用它来分割字符串。

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

答案 1 :(得分:1)

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

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


NSString *fina= [file3 stringByAppendingString:mySongname];

之后你需要像

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

所以你将分别获得时间和歌曲名称。