handling a string for a UITableView

i need to handling this string :

# 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

in a UITableView with differents sections where the title will be the "# ......." and the content "20110330 ........" "20110330 ........" "20110330 ........"

how can i handle this string please?

Answers


Try this thing.

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

Now you have secondArry for section and dictionary of array for rows in sections. P.S. Please ignore syntax error as I'm not using mac machine right now and take care of memory management also.


Try this:

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

Need Your Help

Question regarding factory pattern

c++ design-patterns factory-pattern

I have a factory class to build objects of base class B.

ConnectionManager.AppSettings

c# asp.net sql

I stumbled across an SQL insertion in C# and I am trying to figure the ConnectionManager.Appsetting. Can anyone help me understand the significance of the following code:-