将Plist填充到UITableView,存储在文档目录中

时间:2012-12-12 11:34:09

标签: iphone ios uitableview plist nsdocumentdirectory

我将plist存储在Document Directory中,如果存储在文档目录中,如何调用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;
}

修改 当我输出NSlog valueArray时     ( { pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt546546; title = tyr; } )

这意味着问题在

cellForRowAtIndexPath

1 个答案:

答案 0 :(得分: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;
}