2010-12-10 14 views
0

私はこのプロジェクトを、2列の文字列を持つ単純なplistで開始しました。私は今、より多くの情報を追加すると、このplistの構造を使用したい:plistをTableViewに読み込む

Root - Dictionary - (2 items) 
    Standard - Array - (3 items) 
     Item 0 - Dictionary - (4 items) 
      Color - String - Red 
      Rvalue - String - 255 
      Gvalue - String - 0 
      Bvalue - String - 0 
のplistで入力については申し訳ありません

が、サイトは私がイメージ

を投稿することはできないだろう、私はRGBの値があることができることを知っています文字列ではなく数字ですが、文字列である理由があります。

これは、私はシンプルなplistを読み取るために使用されるコードは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 
    NSString *key = [keys objectAtIndex:section]; 
    NSArray *colorSection = [colors objectForKey:key]; 

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 

    if(cell == nil){ 
     cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SectionsTableIdentifier] autorelease]; 
     } 

    cell.textLabel.text = [colorSection objectAtIndex:row]; 
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows 
    return cell; 
} 

私の質問cell.textLabel.textの色を取得するには、カラー辞書の内容を取得するために特定のコードが何であるかでありますまた、RGB値を読み込んで字幕を追加します。私は数日間この作業をしてきましたが、参考文献とたくさんの例を読んでいて、残念ながら問題を解決することはできません。あなたのお手伝いをさせていただきます。

答えて

1

したがって、.hファイルで定義した配列に対して標準配列を保存すると、このようなものが動作します。この例では、配列はself.coloursArrayに対して格納されます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 

if(cell == nil){ 
    cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SectionsTableIdentifier] autorelease]; 
    } 
NSString* ColourString = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Colour"]; 
NSString* rValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Rvalue"]; 
NSString* gValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Gvalue"]; 
NSString* bValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Bvalue"]; 
cell.textLabel.text = ColourString; 
NSString* subCellString = [NSString stringWithFormat:@"%@:%@:%@", rValue, gValue, bValue]; 
} 

うまくいけばうまくいきます。

1

まず、 - [UITableViewCell initWithFrame:reuseIdentifier:]を使用しないでください。これは廃止され、警告が表示されます。さらに、サブタイトルを実装するのが難しくなります。このコードは、情報をロードし、タイトルをColorプロパティに設定し、Rvalue、Gvalue、およびBvalueプロパティを含む文字列に字幕を設定する、あなたの修正バージョンです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 
    NSString *key = [keys objectAtIndex:section]; 
    NSArray *colorSection = [colors objectForKey:key]; 

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 

    if(cell == nil) { 
     cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: SectionsTableIdentifier] autorelease]; 
    } 

    NSDictionary *color = [colorSection objectAtIndex:row]; 
    cell.textLabel.text = [color objectForKey:@"Color"]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@, %@",[color objectForKey:@"Rvalue"],[color objectForKey:@"Gvalue"],[color objectForKey:@"Bvalue"]]; 
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows 
    return cell; 
} 
関連する問題