从字符串数组中设置UITable中的Detail Text Label

时间:2012-04-29 22:15:54

标签: ios uitableview master-detail detailtextlabel

我正在使用Master-Detail应用程序模板。我硬编码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#”,将详细文本设置为单词列表。我能够为每个单元格设置文本标签,但我似乎无法弄清楚如何传递字符串数组来设置每个细节文本标签。

示例:

第1组

A,然后,发现,去,有

第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;

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

名为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;
}

答案 1 :(得分: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];

答案 2 :(得分:0)

static 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;

我希望这会对你有所帮助......