1

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

编辑 当我NSlog valueArray输出是 ( { pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt; title = tyr; }, { pass = tryrt546546; title = tyr; } )

这意味着问题出在

cellForRowAtIndexPath

4

1 回答 1

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;
}
于 2012-12-12T15:20:35.980 回答