标签:
NSString*str = @"http://app.d1xz.net/xingzuoapp2/news?typ e=xingwen&page=1";
NSURL*url = [NSURLURLWithString:str]; [self createData:url forType:1];//解析数
据
[self.tableView addHeaderWithTarget:self action:@selector(refresh)];
[self refresh];
#pragma mark 下拉刷新 -(void)createData:(NSURL *)url forType:(int)type
{
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"网络错误");
UILabel *label = [[UILabel alloc]
initWithFrame:CGRectMake(kScreenWidth / 3,
kScreenHeight / 2 - 10, kScreenWidth / 3, 40)];
redColor];
label.text = @"网络错误"; label.textColor = [UIColor
[self.view addSubview:label];
}else{
NSDictionary *dataDic = [NSJSONSerialization
JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
NSArray *dataArray = [dataDic objectForKey:@"content"];
NSMutableArray *testArray = [NSMutableArray array];
{
for(NSDictionary *dicindataArray) MessageModel *messageM =
[[MessageModel alloc] init];
[messageM setValuesForKeysWithDictionary:dic];
[testArray addObject:messageM];
}
if (type == 1) {
self.inforArray = testArray;
[self.tableView footerEndRefreshing];
}else { [self.inforArray
addObjectsFromArray:testArray]; [self.tableView
footerEndRefreshing]; }
}
[self.tableView reloadData]; }];if
}
- (void)refresh
{
dispatch_after(dispatch_time(DISPATCH_ TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString *str = @"http://app.d1xz.net/xingzuoapp2/news?typ e=xingwen&page=1";
NSURL *URL = [NSURL URLWithString:str];
[self createData:URL forType:1]; [self.tableView reloadData];
});
[self.tableView headerEndRefreshing]; }
#pragma mark 上拉加载 - (void)loadMore
{
self.pages++;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://app.d1xz.net/xin gzuoapp2/news?type=xingwen&page=%ld",(long )self.pages]];
NSLog(@"%@", url);
[self createData:url forType:2]; }
标签:
原文地址:http://www.cnblogs.com/lidongxiao/p/4949950.html