2010-12-10 49 views
0

我用一個帶有兩個字符串數組的字典的簡單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; 
}