iphone - 显示 Plist 到 UITableview

标签 iphone ios uitableview plist

我想将 Plist 显示到 UITableView 。通过我下面的代码,我可以显示一个键值,即 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

enter the image 是否也可以显示 key name with values

最佳答案

也许使用表格部分...

-(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;
}

关于iphone - 显示 Plist 到 UITableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13723600/

相关文章:

iphone - 这个 UITableView 代码有什么问题,我使用引用传递来更新核心数据配置记录?

ios - 当我将 UIContainerView 添加到 View Controller 时,它的类型是 UIView。如何访问嵌入式 View 的 View Controller ?

ios - 在 UITableViewCell 内的 UICollectionViewCell 上显示 Json。 swift 3

ios - 创建自定义 UITableViewCell 时崩溃

iphone - 如果在添加 subview 后直接将 CATransition 添加到 subview 的图层,为什么 CATransition 不起作用?

iphone - 如何更新 NSUserDefault

iphone - 添加文本到 CALayer

ios - Swift 2.1 词典扩展

ios - Swift countElements() 在计数标志 emoji 时返回不正确的值

iphone - UITableViewCell 文本很长