0

I am working with the Master-Detail Application template. I hard coded an NSArray to set the text label:

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

Then I created two arrays of five words each and put those two arrays into another array:

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

In the tableView:cellForRowAtIndexPath: I am trying to set the cells with the text as "Group#" and the detail text to be the list of words. I am able to set the text label for each cell but I can't seem to figure out how to pass the array of strings to set each detail text label.

Example:

Group 1

A, and, find, go, have

Group 2

here, jump, not, on, one

Here is what I have so far:

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;

Any help is very much appreciated.

d.altman
  • 355
  • 4
  • 15

3 Answers3

2

The variable named detailWords should be an array rather than a string.

To then format the array elements in a line the way you've shown, you could concatenate them into one string, something like:

- (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;
}
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
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];

Edit/Update: My bad, I think I see what you mean now. Try this:

// 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];
mbm29414
  • 11,558
  • 6
  • 56
  • 87
  • Thanks for responding. I'm working with this but so far its not working as needed. It's taking the 1st word from Group1 and placing that as the 1st detail text label for cell 0 and the 2nd word from Group1 as the 1st word for the detail text label for cell 1 and then takes the 1st word from Group2 and places that in cell 0 and the 2nd word from Group2 into cell 1. I want all of Group1 to go into cell 0 and all of Group2 into cell 1. But this does help, I just need to work at it a little more. – d.altman May 01 '12 at 02:36
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;

I hope this will be helpful to you...

Prakash
  • 812
  • 6
  • 16