我存儲在Document Directory
plist中存儲,如何在UITableView
稱,如果它存儲在文檔目錄填充的plist到的UITableView,在文檔目錄
。 這是我從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 當我NSlog
valueArray
輸出是 ({ pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt546546; title = tyr; })
這意味着問題是
cellForRowAtIndexPath
您在.plist中有一系列詞典。 –
ya ..代碼中需要進行哪些更改..help – Christien
這段代碼本身看起來不太好,它假設'self.myData'是'numberOfRowsInSection'字典(行)的數組(字節)的字典,但是是一個'cellForRowAtIndexPath'處的'NSString'數組字典數組字典,這是非常令人困惑的。我認爲如果你首先決定數據源結構會更容易。如果你想將給定的.plist讀入'self.myData'並使用它,目前還不清楚這些數據代表段和行。也許你可以解釋什麼是「傳遞」和「標題」值。另外,如果需要,你可以改變.plist結構嗎? –