UIRefreshControl为什么我无法从我的webApi获取数据

时间:2014-02-01 07:32:31

标签: ios json uirefreshcontrol

- (void)viewDidLoad
{

[super viewDidLoad];
Name = @"Name";
thumbnail = @"Thumbnail";
titlename = @"TitleName";
stw = @"stw";
shopname = @"shopname";
telno = @"telno";
adress = @"adress";
hour = @"hour";
textview = @"textview";

myObject = [[NSMutableArray alloc] init];

NSData *jsonSource = [NSData dataWithContentsOfURL:
                      [NSURL URLWithString:@"http://yy33k.net78.net/json.php"]];

id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  jsonSource options:NSJSONReadingMutableContainers error:nil];

for (NSDictionary *dataDict in jsonObjects) {
    NSString *Name_data = [dataDict objectForKey:@"Name"];
    NSString *thumbnail_data = [dataDict objectForKey:@"Thumbnail"];
    NSString *stw_data = [dataDict objectForKey:@"stw"];
    NSString *titlename_data = [dataDict objectForKey:@"TitleName"];
     NSString *shopname_data = [dataDict objectForKey:@"shopname"];
     NSString *hour_data = [dataDict objectForKey:@"hour"];
     NSString *adress_data = [dataDict objectForKey:@"adress"];
     NSString *telno_data = [dataDict objectForKey:@"telno"];
    NSString *textview_data = [dataDict objectForKey:@"textview"];



    NSLog(@"Name: %@",Name_data);
    NSLog(@"THUMBNAIL: %@",thumbnail_data);
        NSLog(@"AUTHOR: %@",stw_data);
    NSLog(@"titlename: %@",titlename_data);

    dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                  Name_data, Name,
                  thumbnail_data, thumbnail,
                  stw_data,stw,
                  titlename_data, titlename,
                  hour_data, hour,
                  shopname_data, shopname,
                  telno_data, telno,
                  adress_data, adress,
                  textview_data, textview,
                  nil];
    [myObject addObject:dictionary];
}// Do any additional setup after loading the view, typically from a nib.




[self addRefreshViewController];

}


-(void)addRefreshViewController{


self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
[self.refreshControl addTarget:self action:@selector(RefreshViewControlEventValueChanged)
        forControlEvents:UIControlEventValueChanged];
}

 -(void)RefreshViewControlEventValueChanged

{

if (self.refreshControl.refreshing) {

    NSLog(@"refreshing");

    self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"刷新中"];



    [self performSelector:@selector(loadData) withObject:nil afterDelay:0.3];

}

}


 -(void)loadData{


[self.refreshControl endRefreshing];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];


[self.tableView reloadData];
 }

1 个答案:

答案 0 :(得分:0)

以下是refreshControlValueChanged:方法的实现。您还可以在viewDidLoad中重用loadJSON方法。

- (void)refreshControlValueChanged:(UIRefreshControl *)control {

    if (control.refreshing) {
        [self loadJSON];
    }
}

- (void)loadJSON {

    NSURL *jsonURL = [NSURL URLWithString:@"YOUR JSON SOURCE URL STRING HERE"];
    NSURLRequest *request = [NSURLRequest requestWithURL:jsonURL];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        if (!connectionError) {

            NSError *jsonParsingError;

            id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonParsingError];

            if (!jsonParsingError) {

                // Checking for the type of the top level container we received
                if ([obj isKindOfClass:[NSDictionary class]]) {

                    // Got a dictionary, continue processing the data...
                    NSDictionary *myDict = (NSDictionary *)obj;

                }

                // After you've done with processing the received data, you can update the UI here...

                [self.tableView reloadData];
            }
            else {

                // Could not parse the JSON, probably malformed JSON/wrong data was downloaded, handle it!
            }
        }
        else {

            // Got an error while downloading JSON, handle it!
        }

        [self.refreshControl endRefreshing];
    }];
}