您的位置:首页 > 其它

上拉刷新,下拉加载

2015-12-07 23:31 281 查看
#import "AppDelegate.h"

#import "MyTableViewController.h"

@interfaceAppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];

UINavigationController *nvc = [[UINavigationControlleralloc]initWithRootViewController:[[MyTableViewControlleralloc]init]];

self.window.rootViewController = nvc;

[self.windowmakeKeyAndVisible];

returnYES;

}

#import "MyTableViewController.h"

#import "myTableViewCell.h"

#import "UIImageView+WebCache.h"

#import "MJRefresh.h"

NSString *const MJTableViewCellIdentifier = @"Cell";

#define str @"http://apis.juhe.cn/cook/index"

@interfaceMyTableViewController ()<UITableViewDataSource,UITableViewDelegate>

{

NSMutableArray *array;

NSMutableArray *arr;

NSArray *arr1;

MyTableViewController *tabel;

}

@property (strong, nonatomic) NSArray *fakeData;

@end

@implementation MyTableViewController

int i;

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor = [UIColorwhiteColor];

//2.集成刷新控件

self.tableView.rowHeight = 130;

//self.tableView.bounces = NO;

self.tableView.showsVerticalScrollIndicator = NO;

[selfsetupRefresh];

array = [NSMutableArrayarrayWithCapacity:10];

NSString * str1 = [strstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:str1];

NSMutableURLRequest *requst = [NSMutableURLRequestrequestWithURL:url cachePolicy:0timeoutInterval:10];

NSString *data = [NSStringstringWithFormat:@"key=%@&cid=%@&rn=%@",@"3b6ef5c9c2f3dbed7d7d693df8a137cd",@"1",@"30"];

NSData *data1 = [data dataUsingEncoding:NSUTF8StringEncoding];

[requst setHTTPBody:data1];

[requst setHTTPMethod:@"POST"];

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

//解析失败直接解档显示上次缓存数据

// if (connectionError) {

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/str11.txt"];

id obj = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

NSMutableDictionary *dic = obj[@"result"];

arr = dic[@"data"];

NSLog(@"======%@",arr);

for (int i =0; i<5; i++) {

// NSLog(@"-------%@",arr[i]);

[array addObject:arr[i]];

[self.tableView reloadData];

NSLog(@"-----%lu",(unsigned long)array.count);

}

}];

}

/**

* 集成刷新控件

*/

- (void)setupRefresh

{

[self.tableViewaddHeaderWithTarget:selfaction:@selector(headerRereshing) dateKey:@"table"];

// 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)

[self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];

// 设置文字

self.tableView.headerPullToRefreshText = @"下拉可以刷新了";

self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";

self.tableView.headerRefreshingText = @"正在刷新中,请稍等";

self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";

self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";

self.tableView.footerRefreshingText = @"正在加载中,请稍等";

}

- (void)headerRereshing

{

// 2.模拟2秒后刷新表格UI

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

// 刷新表格

[self.tableView reloadData];

// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态

[self.tableViewheaderEndRefreshing];

});

}

- (void)footerRereshing

{

// 1.添加假数据

if (i<5) {

for (int i = 0; i<5; i++) {

[array addObject:arr[i]];

}

[self.tableViewreloadData];

}

i++;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

// 刷新表格

[self.tableView reloadData];

// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态

[self.tableViewfooterEndRefreshing];

});

// [self.tableView footerEndRefreshing];

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSLog(@"111111");

for (NSDictionary * obj in array) {

NSLog(@"=======%@",obj[@"title"]);

}

NSLog(@"++++++%lu",(unsigned long)array.count);

return array.count;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 130;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"2222222");

static NSString *inde = @"cell";

myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inde];

if (cell == nil) {

cell = [[myTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:inde];

}

// myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MJTableViewCellIdentifier forIndexPath:indexPath];

NSDictionary *dic = arr[indexPath.row];

NSArray * arr11 = dic[@"albums"];

[cell.img sd_setImageWithURL:arr11[0] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

}];

cell.lab.text = dic[@"title"];

cell.lab1.text = dic[@"imtro"];

return cell;

}

#import <UIKit/UIKit.h>

@interface myTableViewCell : UITableViewCell

@property(nonatomic,copy)UIImageView *img;

@property(nonatomic,copy)UILabel *lab;

@property(nonatomic,copy)UILabel *lab1;

@property(nonatomic,copy)UILabel *lab2; //此属性用于详细步骤显示页

@end





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: