1

私はこの文字列を処理する必要があります:

# 2012
20120604 Huge Sandstorm over Saudi Arabia
20120527 Huge Sandstorm over niger
# 2012
20110330 Huge Sandstorm over niger
# just for the test
20110330 AHuge Sandstorm over niger
20110330 BHuge Sandstorm over niger
20110330 CHuge Sandstorm over niger
20110330 1Huge Sandstorm over niger
20110330 2Huge Sandstorm over niger
20110330 3Huge Sandstorm over niger
20110330 4Huge Sandstorm over niger
20110330 5CHuge Sandstorm over niger
20110330 6Huge Sandstorm over niger
# **********
20110330 7Huge Sandstorm over niger
20110330 8Huge Sandstorm over niger
20110330 9CHuge Sandstorm over niger
20110330 AHuge Sandstorm over niger
20110330 B10CHuge Sandstorm over niger
20110330 **CHuge Sandstorm over niger

タイトルが「# .......」で、内容が「20110330 ........」「20110330 ........」「20110330 . ……」

この文字列をどのように処理できますか?

4

2 に答える 2

1

このことを試してみてください。

NSString *str = @"above string"; //load above string here
NSArray * firstArry = [str componentsSeparatedByString:@"#"];
NSMutableArray *secondArry = [[NSMutableArray alloc] init]; //Will hold the # titles
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; //Will hold the strings related to # title
for(NSString firstArryStr in firstArry)
  {
      NSArray *tempArry = [firstArryStr componentsSeparatedByString@"\n"];
      NSMutableArray *titleStingsArry = [[NSMutableArray alloc] init];
      for (int i=0; i< [tempArry count];i++) {
            if (i==0) {
                NSString *title = [tempArry  objectAtIndex:i];
                [secondArry addObject:title];
                [dict setValue:titleStingsArry  forKey:title];
            } else {
                [titleStingsArry  addObject:[tempArry  objectAtIndex:i]];
            }
      } 
  }

これsecondArryで、セクション内の行の配列のセクションとディクショナリができました。

PS 現在、Mac マシンを使用していないため、構文エラーは無視してください。また、メモリ管理も行ってください。

于 2012-06-13T09:33:26.037 に答える
0

これを試して:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      if(section == 0) {
       return @"2012";
        } else if (section ==1) {
        return @"# just for the test";
    } // like wise add title for Section header in this method
}

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

    NSString *sCellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sCellIdentifier];
    }

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"Huge Sandstorm over Saudi Arabia";
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"Huge Sandstorm over niger";
        } 
    } else  if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"Huge Sandstorm over niger";
        } 
    } else  if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"AHuge Sandstorm over niger";
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"BHuge Sandstorm over niger";
        }
    } // like wise add text for different section in this method using this conditions
    return cell;
}
于 2012-06-13T09:13:11.290 に答える