【问题标题】:Set the Detail Text Label in a UITable from an array of strings从字符串数组中设置 UITable 中的详细文本标签
【发布时间】:2012-05-09 17:28:12
【问题描述】:

我正在使用主从应用程序模板。我硬编码了一个 NSArray 来设置文本标签:

groups = [[NSArray alloc] initWithObjects:@"Group1", @"Group2", nil];

然后我创建了两个数组,每个数组包含五个单词,并将这两个数组放入另一个数组中:

group1 = [[NSArray alloc] initWithObjects:@"A", @"and", @"find", @"go", @"have", nil];
group2 = [[NSArray alloc] initWithObjects:@"here", @"jump", @"not", @"on", @"one", nil];
groupWords = [[NSArray alloc] initWithObjects:group1, group2, nil];

在 tableView:cellForRowAtIndexPath: 我试图将单元格的文本设置为“Group#”,将详细文本设置为单词列表。我可以为每个单元格设置文本标签,但我似乎无法弄清楚如何传递字符串数组来设置每个详细文本标签。

例子:

第一组

一个,并且,找到,去,拥有

第 2 组

这里,跳,不,上,一个

这是我目前所拥有的:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [groups objectAtIndex:[indexPath row]];

    NSString *detailWords = [self.groupWords objectAtIndex:[indexPath row]];
    NSLog(@"%@", detailWords);

    cell.detailTextLabel.text = 
    return cell;

非常感谢任何帮助。

【问题讨论】:

    标签: ios uitableview master-detail detailtextlabel


    【解决方案1】:
    NSMutableString *detailText = [[NSMutableString alloc] init];
    for (int i = 0; i < [[self groupWords] count]; i = i + 1) {
        NSMutableArray *subArray = [[self groupWords] objectAtIndex:i];
        [detailText appendString:[subArray objectAtIndex:[indexPath row]]];
        if (i < [[self groupWords] count] - 1) [detailText appendString:@","];
    }
    [[cell detailTextLabel] setText:detailText];
    

    编辑/更新: 我的错,我想我现在明白你的意思了。试试这个:

    // Declare the mutable string container
    NSMutableString *detailText = [[NSMutableString alloc] init];
    // Get the array you want to "combine"
    NSArray *array = [[self groupWords] objectAtIndex:[indexPath row]];
    // Iterate the array (this block assumes you've got NSStrings in the array;
    // it might break otherwise)
    for (int i = 0; i < [array count]; i = i + 1) {
        [detailText appendString:[array objectAtIndex:i]];
        if (i < [array count] - 1) [detailText appendString:@", "];
    }
    [[cell detailTextLabel] setText:detailText];
    

    【讨论】:

    • 感谢您的回复。我正在处理这个,但到目前为止它没有按需要工作。它从 Group1 中获取第一个单词并将其作为单元格 0 的第一个详细文本标签,并将 Group1 中的第二个单词作为单元格 1 的详细文本标签的第一个单词,然后从 Group2 中获取第一个单词并将其放置在单元格中0 和 Group2 中的第二个单词进入单元格 1。我希望所有 Group1 进入单元格 0,所有 Group2 进入单元格 1。但这确实有帮助,我只需要多做一点。
    【解决方案2】:

    静态 NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    
    cell.detailTextLabel.text = [[self.groupWords objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
    

    希望对你有帮助...

    【讨论】:

      【解决方案3】:

      名为detailWords 的变量应该是一个数组而不是一个字符串。

      要按照您显示的方式将数组元素格式化为一行,您可以将它们连接成一个字符串,例如:

      - (NSString *)stringFromArrayOfStrings:(NSArray *)array {
          NSMutableString *result = [NSMutableString stringWithString:@""];
          if ([array count] > 0) {
              [result appendString:[array objectAtIndex:0]];
              for (int i = 1; i < [array count]; i++) {
                  [result appendString:@", "];
                  [result appendString:[array objectAtIndex:i]];
              }
          }
          return result;
      }
      

      【讨论】:

      • 感谢您的回复。我正在处理这个,但还没有让它与我的项目一起使用。
      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 2011-10-11
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多