#import "UIScrollView+MJRefresh.h"
@interfacemainViewController ()
@property (strong, nonatomic) NSMutableArray *fakeData;
@end
@implementation mainViewController
/**
* 数据的懒加载
*/
- (NSMutableArray *)fakeData {
if (!_fakeData) {
self.fakeData = [NSMutableArray array];
for (int i = 0; i<12; i++) {
[self.fakeData addObject:[Bdx_note getDict]];
}
}
return_fakeData;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createMainView];
// 1.注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:flag];
// 2.集成刷新控件
[self setupRefresh];
}
-(void)viewWillAppear:(BOOL)animated {
self.navigationController.navigationBarHidden = YES;
}
#pragma mark -tableView
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.fakeData.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *flag = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:flag forIndexPath:indexPath];
[self createCellView:indexPath tableViewCell:cell];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark -refresh
-(void)setupRefresh {
// 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
[_tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//自动刷新(一进入程序就下拉刷新)
//[self.OrderTableView headerBeginRefreshing];
// 设置文字
_tableView.headerPullToRefreshText = @"下拉可以刷新了";
_tableView.headerReleaseToRefreshText = @"松开马上刷新了";
_tableView.headerRefreshingText = @"正在刷新";
[_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
_tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
_tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
_tableView.footerRefreshingText = @"正在加载中";
}
-(void)headerRereshing {
// 1.添加假数据
for (int i = 0; i<5; i++) {
[self.fakeData insertObject:[Bdx_note getDict] atIndex:0];
}
// 2.2秒后刷新表格UI
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 刷新表格
[_tableView reloadData];
// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
[_tableView headerEndRefreshing];
});
}
-(void)footerRereshing {
// 1.添加假数据
for (int i = 0; i<5; i++) {
[self.fakeData addObject:[Bdx_note getDict]];
}
// 2.2秒后刷新表格UI
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 刷新表格
[self.tableView reloadData];
// (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
[self.tableView footerEndRefreshing];
});
}
#pragma mark -initView
-(void)createMainView {
UIView *mainView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
mainView.backgroundColor = [UIColor colorWithRed:182/255.0 green:224/255.0 blue:104/255.0 alpha:1.0];
self.view = mainView;
UIImageView *signal = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"设备信号-0.png"]];
signal.frame = CGRectMake(UISCREEN_WIDTH-50, 30, 30, 30);
signal.userInteractionEnabled = YES;
signal.backgroundColor = [UIColor clearColor];
[mainView addSubview:signal];
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, UISCREEN_WIDTH, UISCREEN_HEIGHT-114) style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor clearColor];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[mainView addSubview:_tableView];
}
-(void)createCellView:(NSIndexPath*)indexPath tableViewCell:(UITableViewCell*)cell {
cell.textLabel.text = [NSString stringWithFormat:@"%@",[NSDate date]];
}
@end
iOS tableView集成(上拉+下拉)刷新
最新推荐文章于 2019-12-04 11:04:55 发布