2012-12-12 81 views
1

我存儲在Document Directory plist中存儲,如何在UITableView稱,如果它存儲在文檔目錄填充的plist到的UITableView,在文檔目錄

eneter image 。 這是我從plist讀取的。

ViewDidLoad

- (void)viewDidLoad { 

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                  NSUserDomainMask, YES); 
NSString *documentsDirectory = [documentPaths objectAtIndex:0]; 
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"p.plist"]; 

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath]; 

NSArray *valueArray = [dict objectForKey:@"title"]; 

self.mySections = valueArray; 

[super viewDidLoad]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
//Return the number of sections. 
return [self.mySections count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
NSString *key = [self.mySections objectAtIndex:section]; 
NSArray *dataInSection = [self.myData objectForKey:key]; 
return [dataInSection count]; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
NSString *key = [self.mySections objectAtIndex:section]; 
return [NSString stringWithFormat:@"%@", key]; 
} 

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
return self.mySections; 
} 


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

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

// Configure the cell... 


NSUInteger section = [indexPath section]; 
NSUInteger row = [indexPath row]; 



NSString *key = [self.mySections objectAtIndex:section]; 

NSDictionary *dataForSection = [[self.myData objectForKey:key] objectAtIndex:0]; 
NSArray *array=dataForSection.allKeys; 

cell.textLabel.text = [[dataForSection allKeys] objectAtIndex:row];  
cell.detailTextLabel.text=[dataForSection valueForKey:[array objectAtIndex:indexPath.row]]; 

return cell; 
} 

EDIT 當我NSlogvalueArray輸出是 ({ pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt546546; title = tyr; })

這意味着問題是

cellForRowAtIndexPath

+0

您在.plist中有一系列詞典。 –

+0

ya ..代碼中需要進行哪些更改..help – Christien

+0

這段代碼本身看起來不太好,它假設'self.myData'是'numberOfRowsInSection'字典(行)的數組(字節)的字典,但是是一個'cellForRowAtIndexPath'處的'NSString'數組字典數組字典,這是非常令人困惑的。我認爲如果你首先決定數據源結構會更容易。如果你想將給定的.plist讀入'self.myData'並使用它,目前還不清楚這些數據代表段和行。也許你可以解釋什麼是「傳遞」和「標題」值。另外,如果需要,你可以改變.plist結構嗎? –

回答

1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
//Return the number of sections. 
return [self.mySections count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
NSArray *allKeys = [[self.mySections objectAtIndex:section] allKeys]; 
return [allKeys count]; 
} 


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

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

// Configure the cell... 


NSUInteger section = [indexPath section]; 
NSUInteger row = [indexPath row]; 
NSArray *allKeys = [[self.mySections objectAtIndex:section] allKeys]; 
cell.textLabel.text = [[self.mySections objectAtIndex:section] objectForKey:[allKeys objectAtIndex:0]];  
cell.detailTextLabel.text=[[self.mySections objectAtIndex:section] objectForKey:[allKeys objectAtIndex:1]]; 

return cell; 
} 
+0

謝謝!它的工作很好now.but問題是...在tableview中...每個值顯示兩次..意味着行1和重複值在第2行爲大家 – Christien

+0

,如果我給返回1;在numberofrowsinsection ...然後輸出很好 – Christien

+0

現在享受... ... - 那個問題是因爲我不知道你到底想要什麼.. :)] – Rajneesh071