【问题标题】:handling a string for a UITableView处理 UITableView 的字符串
【发布时间】:2012-06-13 08:58:32
【问题描述】:

我需要处理这个字符串:

# 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 ........”

请问我该如何处理这个字符串?

【问题讨论】:

    标签: iphone objective-c xcode uitableview nsstring


    【解决方案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 用于部分的行和用于部分中的行的数组字典。

    P.S.请忽略语法错误,因为我现在没有使用 mac 机器并注意内存管理。

    【讨论】:

    • 这就是我想要我尝试的,我会告诉你是否可行 =) 谢谢
    • 这个“for(NSString firstArryStr : firstArry)”有错误
    • @kratosphere 你是对的......实际上我这些天正在使用java,所以预计会有语法错误。 :) 无论如何更新了我的代码
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 你的意思是如果有 50 个这样的文本,那么 @kratosphere 必须写 50 个这样的 if-else 条件?
    • 正如 rishi 所说,通过生成适当的数组和字典,我们可以管理它。
    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多