2010-07-21 39 views
1

我有點難過,我希望有人能幫助我。我有一個plist,是一組字典。我試圖把它讀入表格。 plist中看起來是這樣的:從包含數組的字典中訪問數據

<array> 
    <dict> 
     <key>sectionTitle</key> 
     <string>A</string> 
     <key>rowData</key> 
     <array> 
      <dict> 
       <key>Title</key> 
       <string>Albert Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Antony Author</string> 
       <key>dText</key> 
       <string>Descriptive text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Azerifiel Author</string> 
       <key>dText</key> 
       <string>Text for detail view</string> 
      </dict> 
     </array> 
    </dict> 
    <dict> 
     <key>sectionTitle</key> 
     <string>B</string> 
     <key>rowData</key> 
     <array> 
      <dict> 
       <key>Title</key> 
       <string>Barny Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Bazle Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
      <dict> 
       <key>Title</key> 
       <string>Bruno Author</string> 
       <key>dText</key> 
       <string>text for detail view</string> 
      </dict> 
     </array> 
    </dict> 
</array> 

我的問題是這樣的:我可以做一個類似的結構減去陣列填充表視圖,但我不能讓與陣列工作上面的代碼。我不知道如何處理代碼中的數組,但我無法擺脫它們,因爲我希望對最終表進行索引和分區。以下是我的viewDidLoad方法中的代碼。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Path to the plist (in the application bundle) 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Tester" ofType:@"plist"]; 

    // Build the array from the plist 
    NSArray *dictArray = [[NSArray alloc] initWithContentsOfFile:path]; 
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; 

    for(NSDictionary *dict in dictArray) 
    { 
     DictModel *term = [[DictModel alloc] init]; 
     term.sectionTitle = [dict objectForKey:@"sectionTitle"]; 
     term.Title = [dict objectForKey:@"Title"]; 
     term.dText = [dict objectForKey:@"dText"]; 
     [mutableArray addObject:term]; 
     [term release]; 
    } 

    tableDataSource = [[NSArray alloc] initWithArray:mutableArray]; 
    [mutableArray release]; 
    [dictArray release]; 
    [super viewDidLoad]; 
} 

我有叫DictModel一個單獨的類,它看起來是這樣的:

@interface DictModel : NSObject { 
    NSString *sectionTitle; 
    NSString *Title; 
    NSString *dText; 
} 

@property(nonatomic, retain) NSString *sectionTitle; 
@property(nonatomic,retain) NSString *Title; 
@property(nonatomic, retain) NSString *dText; 

@end 

我要去哪裏錯了?我嘗試導入數組,但無法使其工作。我認爲我在某個地方誤解了某些東西。建議非常感謝。

這是我的行方法的單元格。這可能是顯示問題的地方。我在一個comentator的要求添加了這個:

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell. 
DictModel *term = [tableDataSource objectAtIndex:indexPath.row]; 
cell.textLabel.text = term.Title; 
cell.detailTextLabel.text = term.dText; 

return cell; 

}

+0

那麼究竟是什麼問題呢?您目前是否遇到錯誤或只需要指示如何在編制索引的分組表格中輸出plist? – 2010-07-21 01:37:42

+0

我的表格顯示爲空白。但我知道它正在訪問plist,因爲如果我將rowAtIndex方法指向它,它將顯示sectionTitle字符串。總的來說,我試圖讓我的頭部呈圓形,並且此刻如何訂購/加載它們。 – 2010-07-21 02:35:51

+0

您的表視圖數據源方法被調用。 – Ideveloper 2010-07-21 06:57:01

回答

1

我前陣子解決了這個問題一點從好心人的幫助就在這裏和Mac開發論壇。我以爲我會回來分享我爲iPhone Dev的新手(像我一樣)學習併爲這個問題而努力的經驗。

請注意:寫入最好的我的我所學到試圖解決這個問題的認識下,所以有可能是我的描述了一些技術錯誤。如果你發現任何問題,我會很高興,如果你指出他們。我參與iphone開發,因爲我想學習編程,所以更正歡迎!

首先我的Plist看起來還是一樣的。這是一個字典數組。這實際上是一種更簡單的做我想做的事的方法,而不是使用我剛開始使用的Dictionary of Arrays。之所以我原來的帖子中的代碼看起來很糟糕,是因爲它是基於我理解如何使用一個龐大的數組字典來填充表。

數據源已處理,讓我們進入我的.h文件。它很簡單,看起來像這樣:

@interface MyTableViewController : UITableViewController 
{ 
IBOutlet UITableView *myTableView; 
MyDetailViewController *myDetailViewController; 
NSArray *tableDataSource; 

} 

@property (nonatomic, retain) myDetailViewController *myDetailViewController; 
@property (nonatomic, retain) NSArray *tableDataSource; 

@end 

所有漂亮的自我明顯。我有我的表格視圖和細節表視圖的插座。我還有一個數組,我將把.plist讀入表格中。

移動到我的.m文件看起來像這樣:

@implementation TableViewController 
@synthesize myDetailViewController; 
@synthesize tableDataSource; 

- (void)viewDidLoad { 
[super viewDidLoad]; 

self.title = NSLocalizedString(@"Tester", @"Browse by name"); 

// Path to the plist (in the application bundle) 
NSString *path = [[NSBundle mainBundle] pathForResource:@"Testers" ofType:@"plist"]; 

// Retrieve the plist's root element array 
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path]; 

// Assign the plist array to my instance variable 
self.tableDataSource = array; 
[array release]; 
} 

軸承我原來的問題這一點,我猜重要組成部分,專注於爲viewDidLoad方法。基本上,超級後,我將名稱分配到表視圖的頂部。然後我創建一個名爲「路徑」的字符串,這個字符串是由我的名爲「測試者」的.plist組成的。我創建一個名爲'數組'的數組,並使用之前剛剛創建的名爲「路徑」的字符串的內容對其進行初始化。最後,我將我的.plist數組分配給我的NSArray實例變量'tableDataSource'。這比我最初的嘗試要簡單得多。有一件事我不再需要單獨的類模型。

下一個關鍵的事情就是我的cellForRowAtIndexPathMethod現在看起來是這樣的:

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell. 
NSDictionary *sectionDictionary = [tableDataSource objectAtIndex:indexPath.section]; 
NSArray *sectionRowArray = [sectionDictionary objectForKey:@"rowData"]; 
NSDictionary *rowDictionary = [sectionRowArray objectAtIndex:indexPath.row]; 
cell.textLabel.text = [rowDictionary objectForKey:@"Title"]; 
return cell; 
} 

這是幾乎相同,除了由被配置的小區已經改變,以適應我的閱讀新方式的手段之前來自.plist的數據。所以我不再提到Dict模型。 「rowData」和「Title」是我的plist中的鍵(參見頁首)。 「rowData」是每個部分的關鍵字,「Title」是腳本運行時在單元格中可見的文本。

info的另一個關鍵位是didSelectRowAtIndexPath方法,該方法確定在選擇單元格時將什麼推送到詳細視圖中。它看起來像這樣:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
//Get the dictionary of the selected data source. 
NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex:indexPath.section]objectForKey:@"rowData"] objectAtIndex:indexPath.row]; 

MyDetailViewController *controller = [[MYDetailViewController alloc] initWithNibName:@"MyDetailView" bundle:[NSBundle mainBundle]]; 

controller.title = [dictionary objectForKey:@"Title"]; 
controller.dText = [dictionary objectForKey:@"dText"]; 

[self.navigationController pushViewController:controller animated:YES]; 

[controller release]; 

} 

我開始通過獲取字典這是在我viewDidLoad方法分配給我的實例變量「tableDataSource」表中的數據源。然後我告訴我的視圖細節控制器它在主包中是筆尖,並告訴它,當細節視圖被推入時,它應該把關鍵字「Title」的位置作爲該細節視圖的特定實例的標題,並且它應該使用.plist中的文本在「dText」鍵處填充該nib中的TextView。

好吧,就是這樣。它對我來說工作得很好,儘管我不能發誓這是做這件事的正確方法。感謝那些幫助我最終獲得它的人,我爬上了另一個鼴鼠山!

希望這對其他人有用。

+0

感謝這個很好的解釋。我被困在閱讀另一個字典內的字典數組。乾杯! – nasaa 2012-09-29 15:26:51