显示Plist到UITableview

我想显示PlistUITableView 。通过我的下面的代码,我可以显示一个键值是list of states 。 但我想显示三个表的所有值。

 - (id)readPlist:(NSString *)fileName { NSData *plistData; NSString *error; NSPropertyListFormat format; id plist; NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"]; plistData = [NSData dataWithContentsOfFile:localizedPath]; plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error]; if (!plist) { NSLog(@"Error reading plist from file '%s', error = '%s'", [localizedPath UTF8String], [error UTF8String]); } return plist; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [[[self readPlist:@"sample"] objectForKey:@"ListOfStates"] count]; } - (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]; } cell.textLabel.text = [[[self readPlist:@"sample"] objectForKey:@"ListOfStates"] objectAtIndex:indexPath.row]; return cell;} 

我的输出只有listofkeystates

输入图像 是否也可以显示key名称

也许使用表格部分…

 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { NSDictionary* dict = [self readPlist:@"sample"]; return dict.allKeys.count; } -(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSDictionary* dict = [self readPlist:@"sample"]; return [dict.allKeys objectAtIndex:section]; } -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSDictionary* dict = [self readPlist:@"sample"]; NSString* key = [dict.allKeys objectAtIndex:section]; return [[dict valueForKey:key] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary* dict = [self readPlist:@"sample"]; NSString* key = [dict.allKeys objectAtIndex:indexPath.section]; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = [[dict objectForKey:key] objectAtIndex:indexPath.row]; return cell; } 

只是尝试获取NSDictionaryNSArray数据,然后在UITableView使用它

 NSString *file = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"plist"]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file]; 

并使用tableValue,名字如下:

 NSArray *array = [dict objectForKey:@"ListOfStates"]; NSLog(@"%@", array); 

我希望这可以帮助你..

编辑 :更正了一些错字错误

这个想法

  1. 在dictonary中加载plist
  2. 可选:对它进行sorting(!)
  3. 为每个部分创build数据数组
  4. 创build节的数组

然后执行以下的事情

  1. viewDid load – >在一个字典中加载plist文件,创build一个数组的部分和一个字典

  2. numberOfSectionsInTableView – >节的数量。 在你的情况下,键的数量

  3. numberOfRowsInSection – >每个部分的行数

  4. titleForHeaderInSection – >为每个部分,节的标题

  5. cellForRowAtIndexPath – >所有行的数据

  6. sectionIndexTitlesForTableView – >包含所有部分名称的数组。 在你的情况下,所有的关键

假设您有一个名为mySections的属性和一个名为myData的属性,并假定您使用具有回收标识符“Cell”的标准单元格的Storyboard,

  @synthesize mySections @synthesize myData - (void)viewDidLoad { [super viewDidLoad]; //LOADING THE PLIST FILE //Create a string representing the file path NSString *plistPath = [bundle pathForResource:@"yourFilename" ofType:@"plist"]; //Load the file in a dictionnary NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; self.myData = dict; //SORTING THE DICTIONARY NSArray *dicoArray = [[self.myData allKeys] sortedArrayUsingComparator:^(id firstObject, id secondObject) { return [((NSString *)firstObject) compare:((NSString *)secondObject) options: NSCaseInsensitiveSearch]; }]; self.mySections = dicoArray; } - (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 { NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSString *key = [self.mySections objectAtIndex:section]; NSArray *dataForSection = [self.myData objectForKey:key]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.textLabel.text = [dataForSection objectAtIndex:row]; return cell; }