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

在具有不同部分的 UITableView 中,标题将是 "# ......." 和内容 "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 回答