2012-04-29 10 views
0

私はMaster-Detail Applicationテンプレートを使用しています。その後、私は5つの単語それぞれの二つの配列を作成し、別の配列にそれら二つの配列を置く文字列の配列からUITableの詳細テキストラベルを設定する

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

:のtableViewで

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

:cellForRowAtIndexPath:私はハードテキストラベルを設定したNSArrayをコード化された私は、テキストを "Group#"とし、詳細テキストを単語のリストに設定しようとしています。私は各セルのテキストラベルを設定することができますが、各詳細テキストラベルを設定するために文字列の配列を渡す方法を理解できないようです。

例:

グループ1

A、及び、見つけ、行く、ここ

グループ2

を持って、1

、上のジャンプ、ではないが、ここにありますこれまで私が持っていたもの:

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; 

ご協力いただきまして誠にありがとうございます。

答えて

2

detailWordsという名前の変数は、文字列ではなく配列でなければなりません。

その後、行の配列要素にあなたが示されてきた道をフォーマットするには、1つの文字列にそれらを連結でき、何かのように:

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

感謝。私はこれを使って作業していますが、私のプロジェクトではまだ作業していません。 –

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

お返事ありがとうございます。私はこれで作業していますが、これまでのところ、必要に応じて動作しません。 Group1から1番目の単語を取り出し、セル0の1番目の詳細テキストラベルとGroup1の2番目の単語をセル1の詳細テキストラベルの最初の単語として配置し、Group2から1番目の単語を取り出してセルに配置します0とGroup2の2番目の単語をセル1に追加します。Group1はすべてセル0に、Group2はすべてセル1に移動します。しかし、これは役に立ちます。 –

0

静的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; 

...対応するための

関連する問題