Iphone 将Plist显示到UITableview中
我想将Iphone 将Plist显示到UITableview中,iphone,ios,uitableview,plist,Iphone,Ios,Uitableview,Plist,我想将Plist显示到UITableView中。通过下面的代码,我可以显示一个键值,即状态列表。但是我想显示三个表的所有值 - (id)readPlist:(NSString *)fileName { NSData *plistData; NSString *error; NSPropertyListFormat format; id plist; NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileNam
Plist
显示到UITableView
中。通过下面的代码,我可以显示一个键值,即状态列表
。但是我想显示三个表的所有值
- (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
值
也可以用
值显示键
名称
只需尝试在NSDictionary
和NSArray
中获取数据,然后在UITableView
NSString *file = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file];
并使用tableValue及其名称,如bellow
NSArray *array = [dict objectForKey:@"ListOfStates"];
NSLog(@"%@", array);
我希望这对您有所帮助。也许可以使用表格部分
-(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;
}
编辑:更正了一些打字错误 想法
你到底想要什么?每个表的值中有三个部分,或者是三个表的一个负号?如果您能显示readPlist:的代码和当前得到的输出,那会有所帮助。@Zaphod抱歉,这不是错误。但我只能显示listofstates的键值,即雪兰莪州、丁加奴州、gfh州、,柔佛和马六甲only@SameSungVsIphone我认为JV360的答案可能就是你想要的。但是我确实看到您使用了
propertyListFromData:mutabilityOption:format:errorDescription:
,这已经被弃用了。苹果建议使用propertyListWithData:options:format:error:
@jv360的解决方案最容易列出这三个表。:-)希望能有所帮助。也许有一些打字错误。如果您有问题,请提问。您的代码不适用于我…:(我第一次尝试在Plist中编写代码…无法理解您的代码…mycode是什么?它应该是一个通用代码;-)您收到了什么错误消息?感谢您的工作。但当我将其向下点击时,它会崩溃。我无法在此中添加滚动视图..如何操作?您使用情节提要吗?如果是,只需添加tableview控制器。直接创建tableview控制器,而不是添加表视图的viewcontroller。滚动自动出现在tableview控制器中,无需添加内容。当您滚动以帮助您时,可能会向我显示您收到的错误消息。
@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;
}