【问题标题】:how to read data from plist in tableView when no internet没有互联网时如何从tableView中的plist读取数据
【发布时间】:2013-04-24 06:00:55
【问题描述】:

我从 json url 在 plist 中写入数组。 我想当不存在互联网 tableview 时从 plist 获取数据,当存在时 tableView 从 json url 获取数据

这是我的代码(我在 Document 文件夹应用程序中创建 plist 文件):

#import "ViewController.h"
#define DOC_DIR [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]


@implementation ViewController
{
    NSMutableArray *name;
    NSMutableData *data;
    NSString *listPath;
    NSMutableArray *array;
    NSArray *n;
    NSMutableArray *add;
}
@synthesize table;
-(NSString*)Dir
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURL *url = [NSURL URLWithString:@"http://myDomain.com/test.php"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [con start];


}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    data = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
    [data appendData:theData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    name = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    for (int i = 0; i < [name count]; i++) {

    NSIndexPath *indexPath = [self.table indexPathForSelectedRow];
    n = [[name objectAtIndex:(indexPath.row)+i]objectForKey:@"title"];
    if(!add){
        add = [NSMutableArray array];
    }
    [add addObject:n];
    }
    NSLog(@"add = %@",add);
    [table reloadData];
    [self WriteToPlist:add];
}
- (void)WriteToPlist:(NSArray*)dataArray
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:dataArray,@"Name", nil];
    NSString *Path = [DOC_DIR stringByAppendingPathComponent:@"Plist.plist"];
    [dic writeToFile:Path atomically:YES];
    NSLog(@"Path : %@",Path);
}

这段代码从 json 读取数据:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [name 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];
    }
    cell.textLabel.text = [[name objectAtIndex:indexPath.row] objectForKey:@"title"];
//top code read data from json and I want when no internet read data from plist 
    return cell;
}

【问题讨论】:

    标签: objective-c json uitableview plist


    【解决方案1】:
    // Path to the plist (in the application bundle)
    NSString *path = [[NSBundle mainBundle] pathForResource:
        @"Plist" ofType:@"plist"];
    
    // Build the array from the plist  
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];
    
    // Show the string values  
    for (NSString *str in array2)
      NSLog(@"--%@", str);
    

    你有数组,现在只需将值设置为数据源数组

    [tableview reloadData];
    

    要检查互联网连接,您可以使用Reachability class provided by apple

    【讨论】:

    • 我的朋友在没有互联网的情况下如何在tableView中显示(如何在plist文件中访问?)
    • 为什么需要互联网来访问 plist 文件中的内容?它是一个本地文件。您可以随时访问它。
    • @john Tableview 与连接无关,要检查连接的可用性,请使用 Reachability 类。用链接编辑答案
    • 我的朋友我需要上网检查 ​​plist 如果 plist 不等于 url 更新它,我需要从 plist 显示 tableview
    • 您可以编辑问题并添加您需要的具体内容。正确指出您的需求很重要。欢迎来到 SO 。请阅读常见问题解答
    【解决方案2】:

    第1步:检查Internet是否已连接或未使用以下方法,实施此方法

    - (BOOL) connectedToInternet
    {
        NSURL *requestURL = [NSURL URLWithString:@"http://www.google.com"];
        NSData *data = [NSData dataWithContentsOfURL:requestURL];
    
        return ([data bytes]>0) ? YES : NO;
    }
    

    第 1 步:如果您知道的话,您还可以实现 Reachability Class 来检查 Apple 的 Internet 连接。

    第 2 步:现在在调用 Web 服务请求之前检查您的方法中的连接性

    if([self connectedToInternet])
    {
        // Make a reqeust to server
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        NSURL *url = [NSURL URLWithString:@"http://yourDomain.com/test.php"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [con start];
    }
    else
    {
        // Write a Handy code to load Data from Your Local Plist
        // Path to plist From Documents Folder
        NSString *docFilePath = [DOC_DIR stringByAppendingPathComponent:@"Data.plist"];
    
        NSDictionary *dictData = [NSDictionary dictionaryWithContentsOfFile:docFilePath];
    
    
        NSLog(@"%@",dictData);
        // Reload your TableView
    }
    

    【讨论】:

    • 谢谢你我的朋友你能帮我告诉我如何从文档文件夹中调用 .plist 文件并存储在 NSDictionary 中吗?
    猜你喜欢
    • 1970-01-01
    • 2016-10-10
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多