objective-c – 在UITableView中加载本地json信息
发布时间:2020-12-16 05:06:28  所属栏目:百科  来源:网络整理 
            导读:我有自己的json文件“list.json”,用于下面的示例信息列表.我的json文件是 位于 Xcode内部,我想向表中显示我的信息,请你给我一些提示和帮助,我怎样才能解析本地json并在表中加载信息. [{ "number": "1","name": "jon"},{ "number": "2","name": "Anton"},{ "
                
                
                
            | 
                         我有自己的json文件“list.json”,用于下面的示例信息列表.我的json文件是 
  
位于 Xcode内部,我想向表中显示我的信息,请你给我一些提示和帮助,我怎样才能解析本地json并在表中加载信息. [
{
    "number": "1","name": "jon"
},{
    "number": "2","name": "Anton"
},{
    "number": "9","name": "Lili"
},{
    "number": "7","name": "Kyle"
},{
    "display_number": "8","name": "Linda"
}
]
解决方法
 您可以创建一个继承自UITableViewController的自定义类. 
  
  
        将list.json文件的内容读入数组的代码是: NSString * filePath =[[NSBundle mainBundle] pathForResource:@"list" ofType:@"json"];
    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    if(error)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }
    self.dataList = (NSArray *)[NSJSONSerialization
                                JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                options:0 error:NULL]; 
 头文件是: #import <UIKit/UIKit.h> @interface TVNA_ReadingDataTVCViewController : UITableViewController @end 实施是: #import "TVNA_ReadingDataTVCViewController.h"
@interface TVNA_ReadingDataTVCViewController ()
@property  NSArray* dataList;
@end
@implementation TVNA_ReadingDataTVCViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self readDataFromFile];
    [self.tableView reloadData];
}
-(void)readDataFromFile
{
    NSString * filePath =[[NSBundle mainBundle] pathForResource:@"list" ofType:@"json"];
    NSError * error;
    NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    if(error)
    {
        NSLog(@"Error reading file: %@",error.localizedDescription);
    }
    self.dataList = (NSArray *)[NSJSONSerialization
                                JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
                                options:0 error:NULL];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    id keyValuePair =self.dataList[indexPath.row];
    cell.textLabel.text = keyValuePair[@"name"];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"ID: %@",keyValuePair[@"number"]];
    return cell;
}
@end 
 最后,在您的故事板上,将此类指定为Table View Controller的自定义类.希望这可以帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
